#include <bits/stdc++.h> using namespace std; int t; long long choose(long long x) { //c(x, 3) if(x < 3) return 0; return (x * (x - 1) * (x - 2)) / 6; } long long h(long long p, long long x) { //c(p, 3) + c(p, 2) * (x - p) long long c1 = 0, c2 = 0; if(p >= 3) c1 = (p * (p - 1) * (p - 2)) / 6; if(p >= 2) c2 = (p * (p - 1)) / 2; c2 *= (x - p); return c1 + c2; } long long min_x(long long p, long long games) { long long lo = 0; long long hi = p; while(lo < hi) { long long mid = lo + (hi - lo) / 2; if(h(mid, p) >= games) { hi = mid; } else { lo = mid + 1; } } return lo; } bool check(long long p, long long total, long long max_games, vector<long long>& a) { long long possible = choose(p); if(possible < total || possible < max_games) return false; if(a.size() >= 2 && a[0] > 0 && a.back() > 0) { long long left = min_x(p, a[0]); long long right = min_x(p, a.back()); if(left + right > p) return false; } return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> t; while(t--) { int n; cin >> n; vector<long long> a(n); long long total = 0, max_games = 0; for(int i = 0; i < n; i++) { cin >> a[i]; total += a[i]; max_games = max(max_games, a[i]); } long long lo = 3; long long hi = lo; while(!check(hi, total, max_games, a)) { hi *= 2; if(hi > LLONG_MAX) break; } long long ans = hi; while(lo <= hi) { long long mid = lo + (hi - lo) / 2; if(check(mid, total, max_games, a)) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } cout << ans << '\n'; } }
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 | #include <bits/stdc++.h> using namespace std; int t; long long choose(long long x) { //c(x, 3) if(x < 3) return 0; return (x * (x - 1) * (x - 2)) / 6; } long long h(long long p, long long x) { //c(p, 3) + c(p, 2) * (x - p) long long c1 = 0, c2 = 0; if(p >= 3) c1 = (p * (p - 1) * (p - 2)) / 6; if(p >= 2) c2 = (p * (p - 1)) / 2; c2 *= (x - p); return c1 + c2; } long long min_x(long long p, long long games) { long long lo = 0; long long hi = p; while(lo < hi) { long long mid = lo + (hi - lo) / 2; if(h(mid, p) >= games) { hi = mid; } else { lo = mid + 1; } } return lo; } bool check(long long p, long long total, long long max_games, vector<long long>& a) { long long possible = choose(p); if(possible < total || possible < max_games) return false; if(a.size() >= 2 && a[0] > 0 && a.back() > 0) { long long left = min_x(p, a[0]); long long right = min_x(p, a.back()); if(left + right > p) return false; } return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> t; while(t--) { int n; cin >> n; vector<long long> a(n); long long total = 0, max_games = 0; for(int i = 0; i < n; i++) { cin >> a[i]; total += a[i]; max_games = max(max_games, a[i]); } long long lo = 3; long long hi = lo; while(!check(hi, total, max_games, a)) { hi *= 2; if(hi > LLONG_MAX) break; } long long ans = hi; while(lo <= hi) { long long mid = lo + (hi - lo) / 2; if(check(mid, total, max_games, a)) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } cout << ans << '\n'; } } |