#include <bits/stdc++.h>
using namespace std;
bool check(int total, int n, vector <int> &a) {
int pref = 0;
for (int i = 0; i < n; i++) {
int x = 0;
while (true) {
long long maxMeet = (
(long long) pref * x * (total - pref - x) +
(long long) pref * x * (x - 1) / 2 +
(long long) (total - pref - x) * x * (x - 1) / 2 +
(long long) x * (x - 1) * (x - 2) / 6
);
if (maxMeet >= a[i]) {
break;
}
x++;
}
pref += x;
if (pref > total) {
return false;
}
}
return true;
}
int solve(int n, vector <int> &a) {
const int MX = 1e6;
int l = 0, r = MX;
while (r - l > 1) {
int mid = (l + r) / 2;
if (check(mid, n, a)) {
r = mid;
} else {
l = mid;
}
}
return r;
}
int main() {
ios_base::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector <int> a(n);
for (int &ai : a) {
cin >> ai;
}
cout << solve(n, a) << '\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 59 60 61 62 63 64 65 66 67 68 69 70 | #include <bits/stdc++.h> using namespace std; bool check(int total, int n, vector <int> &a) { int pref = 0; for (int i = 0; i < n; i++) { int x = 0; while (true) { long long maxMeet = ( (long long) pref * x * (total - pref - x) + (long long) pref * x * (x - 1) / 2 + (long long) (total - pref - x) * x * (x - 1) / 2 + (long long) x * (x - 1) * (x - 2) / 6 ); if (maxMeet >= a[i]) { break; } x++; } pref += x; if (pref > total) { return false; } } return true; } int solve(int n, vector <int> &a) { const int MX = 1e6; int l = 0, r = MX; while (r - l > 1) { int mid = (l + r) / 2; if (check(mid, n, a)) { r = mid; } else { l = mid; } } return r; } int main() { ios_base::sync_with_stdio(false); int t; cin >> t; while (t--) { int n; cin >> n; vector <int> a(n); for (int &ai : a) { cin >> ai; } cout << solve(n, a) << '\n'; } return 0; } |
English