#include <bits/stdc++.h> using namespace std; typedef long long ll; #define vc vector #define st first #define nd second #define pll pair<ll, ll> #define sz(a) (ll)a.size() #define all(a) a.begin(), a.end() #define pu push #define pub push_back #define pob pop_back #define em emplace #define emb emplace_back const ll INF = 2e18; const ll B = 50'000'000; ll R = 183; ll n, m; vc<ll> a; void input() { cin >> n; a.resize(n); for (ll &ai : a) cin >> ai; } ll ceil(ll x, ll y) { return (x + y - 1) / y; } bool check() { ll p = 0; R = 183; for (ll x = 1; x <= 200; x++) { ll w = x * (x - 1) * (x - 2) / 6; w += x * (x - 1) / 2 * (m - x); if (w >= 1'000'000 or w < 0) { R = x; break; } } for (ll i = 0; i < n; i++) { if (a[i] == 0) continue; if (i == 0) { for (ll x = 1; x <= R + 1; x++) { if (x == R + 1) return false; ll w = x * (x - 1) * (x - 2) / 6; w += x * (x - 1) / 2 * (m - x); if (w >= a[i]) { p = x; break; } } if (p > m) return false; continue; } if (i == n - 1) { for (ll x = 1; x <= R + 1; x++) { if (x == R + 1) return false; ll w = x * (x - 1) * (x - 2) / 6; w += x * (x - 1) / 2 * (m - x); if (w >= a[i]) { p += x; break; } } if (p > m) return false; continue; } ll np = INF; for (ll x = 1; x <= R; x++) { ll w = x * (x - 1) * (x - 2) / 6; w += x * (x - 1) / 2 * (m - x); ll l = p, r = max((m - x) / 2, p) + 1; while (l < r) { ll s = (l + r) / 2; if (w + s * x * (m - x - s) >= a[i]) r = s; else l = s + 1; } if (l > max((m - x) / 2, p)) continue; np = min(np, l + x); } if (np > m) return false; p = np; } return true; } ll binsearch() { if (n == 1) { for (ll x = 1; x <= R; x++) { if (x * (x - 1) * (x - 2) / 6 >= a[0]) return x; } } ll pos = 0; for (ll &ai : a) if (ai > 0) pos++; ll l = pos, r = B; while (l < r) { m = (l + r) / 2; if (check()) r = m; else l = m + 1; } return l; } void program() { input(); cout << binsearch() << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll T = 1; cin >> T; while (T--) program(); 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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #define vc vector #define st first #define nd second #define pll pair<ll, ll> #define sz(a) (ll)a.size() #define all(a) a.begin(), a.end() #define pu push #define pub push_back #define pob pop_back #define em emplace #define emb emplace_back const ll INF = 2e18; const ll B = 50'000'000; ll R = 183; ll n, m; vc<ll> a; void input() { cin >> n; a.resize(n); for (ll &ai : a) cin >> ai; } ll ceil(ll x, ll y) { return (x + y - 1) / y; } bool check() { ll p = 0; R = 183; for (ll x = 1; x <= 200; x++) { ll w = x * (x - 1) * (x - 2) / 6; w += x * (x - 1) / 2 * (m - x); if (w >= 1'000'000 or w < 0) { R = x; break; } } for (ll i = 0; i < n; i++) { if (a[i] == 0) continue; if (i == 0) { for (ll x = 1; x <= R + 1; x++) { if (x == R + 1) return false; ll w = x * (x - 1) * (x - 2) / 6; w += x * (x - 1) / 2 * (m - x); if (w >= a[i]) { p = x; break; } } if (p > m) return false; continue; } if (i == n - 1) { for (ll x = 1; x <= R + 1; x++) { if (x == R + 1) return false; ll w = x * (x - 1) * (x - 2) / 6; w += x * (x - 1) / 2 * (m - x); if (w >= a[i]) { p += x; break; } } if (p > m) return false; continue; } ll np = INF; for (ll x = 1; x <= R; x++) { ll w = x * (x - 1) * (x - 2) / 6; w += x * (x - 1) / 2 * (m - x); ll l = p, r = max((m - x) / 2, p) + 1; while (l < r) { ll s = (l + r) / 2; if (w + s * x * (m - x - s) >= a[i]) r = s; else l = s + 1; } if (l > max((m - x) / 2, p)) continue; np = min(np, l + x); } if (np > m) return false; p = np; } return true; } ll binsearch() { if (n == 1) { for (ll x = 1; x <= R; x++) { if (x * (x - 1) * (x - 2) / 6 >= a[0]) return x; } } ll pos = 0; for (ll &ai : a) if (ai > 0) pos++; ll l = pos, r = B; while (l < r) { m = (l + r) / 2; if (check()) r = m; else l = m + 1; } return l; } void program() { input(); cout << binsearch() << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ll T = 1; cin >> T; while (T--) program(); return 0; } |