#include <bits/stdc++.h> using namespace std; #define FOR(i,l,r) for(int i = (l); i <= (r); i++) #define FORD(i,l,r) for(int i = (l); i >= (r); i--) using num = double; using ind = long long; bool try_solve(vector<int>& v, ind k){ ind prev=0; ind future=k; for(ind x : v){ if((k)*(k-1)*(k-2)/6 - prev*(prev-1)*future/2 - prev*(prev-1)*(prev-2)/6 < x){ return false; } ind start = 1; ind end = min(250'000ll,future); while(start < end){ ind center = (start+end)/2; if(prev*(center)*(future-center) + center*(center-1)*(prev+future-center)/2 +(center)*(center-1)*(center-2)/6 >= x){ end = center; }else{ start = center+1; } } prev += start; future -= start; } return true; } void solve(){ int n; cin >> n; vector<int> v; FOR(i,1,n){ int a; cin >> a; if(a > 0){ v.push_back(a); } } if(v.size()==0){ cout << 0 << '\n'; return; } ind start = 1; ind end = 250'000; while(start < end){ ind center = (start+end)/2; if(try_solve(v, center)){ end = center; }else{ start = center+1; } } cout << start << '\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--){ solve(); } }
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 | #include <bits/stdc++.h> using namespace std; #define FOR(i,l,r) for(int i = (l); i <= (r); i++) #define FORD(i,l,r) for(int i = (l); i >= (r); i--) using num = double; using ind = long long; bool try_solve(vector<int>& v, ind k){ ind prev=0; ind future=k; for(ind x : v){ if((k)*(k-1)*(k-2)/6 - prev*(prev-1)*future/2 - prev*(prev-1)*(prev-2)/6 < x){ return false; } ind start = 1; ind end = min(250'000ll,future); while(start < end){ ind center = (start+end)/2; if(prev*(center)*(future-center) + center*(center-1)*(prev+future-center)/2 +(center)*(center-1)*(center-2)/6 >= x){ end = center; }else{ start = center+1; } } prev += start; future -= start; } return true; } void solve(){ int n; cin >> n; vector<int> v; FOR(i,1,n){ int a; cin >> a; if(a > 0){ v.push_back(a); } } if(v.size()==0){ cout << 0 << '\n'; return; } ind start = 1; ind end = 250'000; while(start < end){ ind center = (start+end)/2; if(try_solve(v, center)){ end = center; }else{ start = center+1; } } cout << start << '\n'; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--){ solve(); } } |