#include <bits/stdc++.h>
using namespace std;
long long newton(int n) {
if (n < 3)
return 0;
return (1LL * n * (n - 1) * (n - 2)) / 6;
}
bool mozliwe(int n, const vector<int> &vec) {
long long calk = newton(n);
long long suma = 0;
long long kara = 0;
long long limit = (n == 3 ? newton(n) : newton(n - 1));
int pelne = 0;
for (int a : vec) {
suma += a;
if (a > limit) {
kara += (a - limit);
} else if (a == limit) {
pelne++;
}
}
if (pelne > 0) {
kara += (pelne - 1LL) * (n - 2);
}
return calk >= (suma + kara);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> vec(n);
for (int i = 0; i < n; i++) {
cin >> vec[i];
}
int l = 3, p = 200000, wynik = p;
while (l <= p) {
int s = (l + p) / 2;
if (mozliwe(s, vec)) {
wynik = s;
p = s - 1;
} else {
l = s + 1;
}
}
cout << wynik << '\n';
}
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include <bits/stdc++.h> using namespace std; long long newton(int n) { if (n < 3) return 0; return (1LL * n * (n - 1) * (n - 2)) / 6; } bool mozliwe(int n, const vector<int> &vec) { long long calk = newton(n); long long suma = 0; long long kara = 0; long long limit = (n == 3 ? newton(n) : newton(n - 1)); int pelne = 0; for (int a : vec) { suma += a; if (a > limit) { kara += (a - limit); } else if (a == limit) { pelne++; } } if (pelne > 0) { kara += (pelne - 1LL) * (n - 2); } return calk >= (suma + kara); } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> vec(n); for (int i = 0; i < n; i++) { cin >> vec[i]; } int l = 3, p = 200000, wynik = p; while (l <= p) { int s = (l + p) / 2; if (mozliwe(s, vec)) { wynik = s; p = s - 1; } else { l = s + 1; } } cout << wynik << '\n'; } return 0; } |
English