// Karol Kosinski 2025 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define X first #define Y second #define endl '\n' #define NAM(x) #x,'=',x," " using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; template<class...T> void _cout(T...a){(cout<<...<<a);} #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)_cout("------",setw(4),__LINE__," : ",__FUNCTION__,endl);if(p)f(x);} #endif #define DEBF(f,x...) DEB(1,1,f,x) #define DEBL DEBF(void,0) #define DEBC(p,x...) DEB(0,p,_cout,x) #define DEBUG(x...) DEBC(1,x) constexpr int NX = 200'003; constexpr int VX = 200'011; LL A[NX], D[NX]; LL Newt2[VX], Newt3[VX]; void preproc() { FOR(i,2,VX) { Newt2[i] = LL(i) * (i-1) / 2; Newt3[i] = LL(i) * (i-1) * (i-2) / 6; // DEBUG( "", NAM(i), ": ", Newt3[i], endl ); } DEBL; } bool run(int limit, int b, int e) { DEBUG( "+- ", NAM(limit), endl ); { DEBUG( "+-+- [entry] ", NAM(A[b]), "<= ", Newt3[limit-1] + Newt2[limit-1], endl ); if ( A[b] > Newt3[limit-1] + Newt2[limit-1] ) return false; int x = 1, y = limit - 1; while ( x < y ) { int k = ( x + y ) / 2; DEBUG( "++++ ", NAM(x), NAM(y), " --> ", NAM(k), endl ); LL val = Newt3[k] + Newt2[k] * ( limit - k ); if ( A[b] <= val ) y = k; else x = k + 1; DEBUG( "+-+- ", NAM(A[b]), "<= ", val, endl ); } D[b] = y; DEBUG( "++++ final: ", NAM(y), NAM(D[b]), endl ); } FR_(i,b+1,e-1) { DEBL; if ( A[i] == 0 ) { D[i] = D[i-1]; continue; } DEBUG( "++++ [entry] ", NAM(i), ": 2 <= ", limit - D[i-1], endl ); if ( 2 > limit - D[i-1] ) return false; LL LV = D[i-1], RV = limit - D[i-1]; LL x = 1, y = RV - 1; DEBUG( "++++ ", NAM(x), NAM(y), endl ); if ( 1 > y - x ) return false; { LL val = Newt3[y] + Newt2[y] * ( limit - y ) + LV * ( RV - y ) * y; DEBUG( "+-+- [entry] ", NAM(A[i]), "<= ", val, endl ); if ( A[i] > val ) return false; } while ( x < y ) { LL k = ( x + y ) / 2; DEBUG( "++++ ", NAM(x), NAM(y), " --> ", NAM(k), endl ); LL val = Newt3[k] + Newt2[k] * ( limit - k ) + LV * ( RV - k ) * k; if ( A[i] <= val ) y = k; else x = k + 1; DEBUG( "+-+- ", NAM(A[i]), "<= ", val, endl ); } D[i] = D[i-1] + y; DEBUG( "++++ final: ", NAM(y), NAM(D[i]), endl ); } DEBL; int rd = limit - D[e-1]; DEBUG( "+-+- [entry] ", NAM(A[e]), "<= ", Newt3[rd] + Newt2[rd] * ( limit - rd ), endl ); return A[e] <= Newt3[rd] + Newt2[rd] * ( limit - rd ); } void solve() { int n; cin >> n; FOR(i,0,n) cin >> A[i]; int b = 0, e = n - 1; while ( A[b] == 0 ) ++ b; while ( A[e] == 0 ) -- e; if ( b == e ) { int limit = lower_bound( Newt3, Newt3 + VX, A[b] ) - Newt3; cout << limit << endl; return; } int x = 2, y = VX - 1; FR_(i,b,e) if ( A[i] ) ++ x; DEBL; while ( x < y ) { int k = ( x + y ) / 2; DEBUG( "** ", NAM(x), NAM(y), " --> ", NAM(k), endl ); if ( run(k,b,e) ) y = k; else x = k + 1; DEBUG( endl ); } cout << y << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); preproc(); int z; cin >> z; while ( z -- ) solve(); 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | // Karol Kosinski 2025 #include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i) #define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) int((c).size()) #define X first #define Y second #define endl '\n' #define NAM(x) #x,'=',x," " using namespace std; using LL = long long; using ULL = unsigned long long; using PII = pair<int, int>; using TIII = tuple<int, int, int>; template<class...T> void _cout(T...a){(cout<<...<<a);} #ifndef ENABLE_DEBUG #define DEB(k,p,f,x...) #else #define DEB(k,p,f,x...) {if(k)_cout("------",setw(4),__LINE__," : ",__FUNCTION__,endl);if(p)f(x);} #endif #define DEBF(f,x...) DEB(1,1,f,x) #define DEBL DEBF(void,0) #define DEBC(p,x...) DEB(0,p,_cout,x) #define DEBUG(x...) DEBC(1,x) constexpr int NX = 200'003; constexpr int VX = 200'011; LL A[NX], D[NX]; LL Newt2[VX], Newt3[VX]; void preproc() { FOR(i,2,VX) { Newt2[i] = LL(i) * (i-1) / 2; Newt3[i] = LL(i) * (i-1) * (i-2) / 6; // DEBUG( "", NAM(i), ": ", Newt3[i], endl ); } DEBL; } bool run(int limit, int b, int e) { DEBUG( "+- ", NAM(limit), endl ); { DEBUG( "+-+- [entry] ", NAM(A[b]), "<= ", Newt3[limit-1] + Newt2[limit-1], endl ); if ( A[b] > Newt3[limit-1] + Newt2[limit-1] ) return false; int x = 1, y = limit - 1; while ( x < y ) { int k = ( x + y ) / 2; DEBUG( "++++ ", NAM(x), NAM(y), " --> ", NAM(k), endl ); LL val = Newt3[k] + Newt2[k] * ( limit - k ); if ( A[b] <= val ) y = k; else x = k + 1; DEBUG( "+-+- ", NAM(A[b]), "<= ", val, endl ); } D[b] = y; DEBUG( "++++ final: ", NAM(y), NAM(D[b]), endl ); } FR_(i,b+1,e-1) { DEBL; if ( A[i] == 0 ) { D[i] = D[i-1]; continue; } DEBUG( "++++ [entry] ", NAM(i), ": 2 <= ", limit - D[i-1], endl ); if ( 2 > limit - D[i-1] ) return false; LL LV = D[i-1], RV = limit - D[i-1]; LL x = 1, y = RV - 1; DEBUG( "++++ ", NAM(x), NAM(y), endl ); if ( 1 > y - x ) return false; { LL val = Newt3[y] + Newt2[y] * ( limit - y ) + LV * ( RV - y ) * y; DEBUG( "+-+- [entry] ", NAM(A[i]), "<= ", val, endl ); if ( A[i] > val ) return false; } while ( x < y ) { LL k = ( x + y ) / 2; DEBUG( "++++ ", NAM(x), NAM(y), " --> ", NAM(k), endl ); LL val = Newt3[k] + Newt2[k] * ( limit - k ) + LV * ( RV - k ) * k; if ( A[i] <= val ) y = k; else x = k + 1; DEBUG( "+-+- ", NAM(A[i]), "<= ", val, endl ); } D[i] = D[i-1] + y; DEBUG( "++++ final: ", NAM(y), NAM(D[i]), endl ); } DEBL; int rd = limit - D[e-1]; DEBUG( "+-+- [entry] ", NAM(A[e]), "<= ", Newt3[rd] + Newt2[rd] * ( limit - rd ), endl ); return A[e] <= Newt3[rd] + Newt2[rd] * ( limit - rd ); } void solve() { int n; cin >> n; FOR(i,0,n) cin >> A[i]; int b = 0, e = n - 1; while ( A[b] == 0 ) ++ b; while ( A[e] == 0 ) -- e; if ( b == e ) { int limit = lower_bound( Newt3, Newt3 + VX, A[b] ) - Newt3; cout << limit << endl; return; } int x = 2, y = VX - 1; FR_(i,b,e) if ( A[i] ) ++ x; DEBL; while ( x < y ) { int k = ( x + y ) / 2; DEBUG( "** ", NAM(x), NAM(y), " --> ", NAM(k), endl ); if ( run(k,b,e) ) y = k; else x = k + 1; DEBUG( endl ); } cout << y << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); preproc(); int z; cin >> z; while ( z -- ) solve(); return 0; } |