#include <cstdio> #include <algorithm> #define D(x) using namespace std; #define MAX 200100 typedef long long ll; int a[MAX],t,n; static int testX(ll A, ll x, ll sr, int yy) { ll B = sr - A - x; ll y = A*x*B; if (x >= 2) y += x*(x-1)*(A+B)/2; if (x >= 3) y += x*(x-1)*(x-2)/6; if (y >= yy) return true; return false; } static int findX(int from, int to, ll A, ll sr, int yy) { int x = -1; while(from <= to) { int ft2 = (from+to)/2; if (testX(A,ft2,sr,yy)) { x = ft2; to = ft2-1; } else { from = ft2 + 1; } } return x; } static bool test(ll sr) { ll A = 0; for(int i=0;i<n;i++) { ll to = min(200LL, sr - A); if (to < 0) return false; int x = findX(0,to, A, sr, a[i]); // printf("fx: %d to: %d\n", x, to); if (x < 0) return false; A+=x; } return true; } int main() { scanf("%d", &t); while(t--) { ll sa = 0, sr = 0; scanf("%d", &n); for(int i=0;i<n;i++) { scanf("%d", &a[i]); sa += a[i]; } ll from = 0, to = 201008; while(from <= to) { int ft2 = (from+to)/2; if (test(ft2)) { sr = ft2; to = ft2-1; } else { from = ft2 + 1; } } printf("%lld\n",sr); } }
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 | #include <cstdio> #include <algorithm> #define D(x) using namespace std; #define MAX 200100 typedef long long ll; int a[MAX],t,n; static int testX(ll A, ll x, ll sr, int yy) { ll B = sr - A - x; ll y = A*x*B; if (x >= 2) y += x*(x-1)*(A+B)/2; if (x >= 3) y += x*(x-1)*(x-2)/6; if (y >= yy) return true; return false; } static int findX(int from, int to, ll A, ll sr, int yy) { int x = -1; while(from <= to) { int ft2 = (from+to)/2; if (testX(A,ft2,sr,yy)) { x = ft2; to = ft2-1; } else { from = ft2 + 1; } } return x; } static bool test(ll sr) { ll A = 0; for(int i=0;i<n;i++) { ll to = min(200LL, sr - A); if (to < 0) return false; int x = findX(0,to, A, sr, a[i]); // printf("fx: %d to: %d\n", x, to); if (x < 0) return false; A+=x; } return true; } int main() { scanf("%d", &t); while(t--) { ll sa = 0, sr = 0; scanf("%d", &n); for(int i=0;i<n;i++) { scanf("%d", &a[i]); sa += a[i]; } ll from = 0, to = 201008; while(from <= to) { int ft2 = (from+to)/2; if (test(ft2)) { sr = ft2; to = ft2-1; } else { from = ft2 + 1; } } printf("%lld\n",sr); } } |