#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sz(x) (int)(x).size()
bool solve(const vector<int>& a, ll m) {
int n = sz(a);
ll l = 0, // before current house
k = 0, // at current house
r = m; // after current house
auto res = [&]() {
return (k * l * r) + (k * (k - 1) / 2 * (l + r)) +(k * (k - 1) * (k - 2) / 6);
};
for (int i = 0; i < n; i++) {
while (res() < a[i]) {
k++;
r--;
if (r < 0)
return false;
}
l += k;
k = 0;
}
return true;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin.exceptions(cin.failbit);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n);
for (auto& x : a)
cin >> x;
a.erase(remove(a.begin(), a.end(), 0), a.end());
n = sz(a);
int first = 3, last = 1e6;
while (first <= last) {
int mid = (last + first) / 2;
if (solve(a, mid))
last = mid - 1;
else
first = mid + 1;
}
cout << first << '\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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; #define sz(x) (int)(x).size() bool solve(const vector<int>& a, ll m) { int n = sz(a); ll l = 0, // before current house k = 0, // at current house r = m; // after current house auto res = [&]() { return (k * l * r) + (k * (k - 1) / 2 * (l + r)) +(k * (k - 1) * (k - 2) / 6); }; for (int i = 0; i < n; i++) { while (res() < a[i]) { k++; r--; if (r < 0) return false; } l += k; k = 0; } return true; } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n); for (auto& x : a) cin >> x; a.erase(remove(a.begin(), a.end(), 0), a.end()); n = sz(a); int first = 3, last = 1e6; while (first <= last) { int mid = (last + first) / 2; if (solve(a, mid)) last = mid - 1; else first = mid + 1; } cout << first << '\n'; } } |
English