// PA2025, @mjm, r5c-tur
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <functional>
using namespace std;
using ull = unsigned long long;
inline int nextInt() { int n; scanf("%d", &n); return n; }
inline ull nextUll() { ull n; scanf("%llu", &n); return n; }
inline int myMin(int a, int b) { return a <= b ? a : b; }
inline ull myMax(ull a, ull b) { return a >= b ? a : b; }
const int N = 200000 + 9;
int n;
ull a[N];
bool myTest(int cnt) {
int done = 0;
int todo = cnt;
for (int i = 0; i < n; ++i) {
const ull ZERO = 0;
const ull ONE = 1;
if (ZERO == a[i]) continue;
int found = -1;
int maxX = myMin(todo, 200);
for (int x=1; x<=maxX; ++x) {
ull cur = 0;
// ABC
cur += ONE * done * x * (todo - x);
// ABB + BBC
cur += ONE * x * (x - 1) * (cnt - x) / 2;
// BBB
cur += ONE * x * (x - 1) * (x - 2) / 6;
if (cur >= a[i]) {
found = x;
break;
}
}
if (found < 0) return false;
done += found;
todo -= found;
}
return true;
}
int solve() {
int a = 2;
int b = n * 200;
while (a + 1 < b) {
int c = (a + b) / 2;
bool ok = myTest(c);
if (ok)
b = c;
else
a = c;
}
return b;
}
int main() {
int TC = nextInt();
for (int tc = 0; tc < TC; ++tc) {
n = nextInt();
for (int i = 0; i < n; ++i)
a[i] = nextInt();
int res = solve();
printf("%d\n", res);
}
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 71 72 73 74 75 76 77 78 79 80 | // PA2025, @mjm, r5c-tur #include <cstdio> #include <string> #include <vector> #include <queue> #include <set> #include <map> #include <unordered_map> #include <algorithm> #include <functional> using namespace std; using ull = unsigned long long; inline int nextInt() { int n; scanf("%d", &n); return n; } inline ull nextUll() { ull n; scanf("%llu", &n); return n; } inline int myMin(int a, int b) { return a <= b ? a : b; } inline ull myMax(ull a, ull b) { return a >= b ? a : b; } const int N = 200000 + 9; int n; ull a[N]; bool myTest(int cnt) { int done = 0; int todo = cnt; for (int i = 0; i < n; ++i) { const ull ZERO = 0; const ull ONE = 1; if (ZERO == a[i]) continue; int found = -1; int maxX = myMin(todo, 200); for (int x=1; x<=maxX; ++x) { ull cur = 0; // ABC cur += ONE * done * x * (todo - x); // ABB + BBC cur += ONE * x * (x - 1) * (cnt - x) / 2; // BBB cur += ONE * x * (x - 1) * (x - 2) / 6; if (cur >= a[i]) { found = x; break; } } if (found < 0) return false; done += found; todo -= found; } return true; } int solve() { int a = 2; int b = n * 200; while (a + 1 < b) { int c = (a + b) / 2; bool ok = myTest(c); if (ok) b = c; else a = c; } return b; } int main() { int TC = nextInt(); for (int tc = 0; tc < TC; ++tc) { n = nextInt(); for (int i = 0; i < n; ++i) a[i] = nextInt(); int res = solve(); printf("%d\n", res); } return 0; } |
English