#include <bits/stdc++.h>
#define nl '\n'
using namespace std;
void solve()
{
int n;
cin>>n;
deque<int> req(n);
for(int i=0; i<n; i++){ cin>>req[i]; }
while(!req.front()) req.pop_front();
while(!req.back()) req.pop_back();
n = req.size();
int res = n+2;
vector<int> people(n+1, 1);
people[1]++;
people[n]++;
while(true){
vector<int> prefix(n+1, 0), suffix(n+2, 0);
for(int i=1; i<=n; i++){
prefix[i] = prefix[i-1] + people[i];
}
for(int i=n; i>0; i--){
suffix[i] = suffix[i+1] + people[i];
}
int req_max = 0;
int req_pos = -1;
for(int i=1; i<=n; i++){
int matches = prefix[i-1] * suffix[i+1] * people[i] + people[i] * (people[i] - 1) /2 * (prefix[i-1] + suffix[i+1]) + people[i] * (people[i] -1 ) * (people[i] - 2) / 6;
//cerr<<i<<' '<<matches<<nl;
if(matches >= req[i-1]) continue;
if(req[i-1] - matches > req_max){
req_max = req[i-1] - matches;
req_pos = i;
}
}
if(req_max){
//cerr<<req_max<<' '<<req_pos<<nl;
res++;
people[req_pos]++;
}else{
break;
}
}
cout<<res<<nl;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
int t;
cin>>t;
while(t--){
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 | #include <bits/stdc++.h> #define nl '\n' using namespace std; void solve() { int n; cin>>n; deque<int> req(n); for(int i=0; i<n; i++){ cin>>req[i]; } while(!req.front()) req.pop_front(); while(!req.back()) req.pop_back(); n = req.size(); int res = n+2; vector<int> people(n+1, 1); people[1]++; people[n]++; while(true){ vector<int> prefix(n+1, 0), suffix(n+2, 0); for(int i=1; i<=n; i++){ prefix[i] = prefix[i-1] + people[i]; } for(int i=n; i>0; i--){ suffix[i] = suffix[i+1] + people[i]; } int req_max = 0; int req_pos = -1; for(int i=1; i<=n; i++){ int matches = prefix[i-1] * suffix[i+1] * people[i] + people[i] * (people[i] - 1) /2 * (prefix[i-1] + suffix[i+1]) + people[i] * (people[i] -1 ) * (people[i] - 2) / 6; //cerr<<i<<' '<<matches<<nl; if(matches >= req[i-1]) continue; if(req[i-1] - matches > req_max){ req_max = req[i-1] - matches; req_pos = i; } } if(req_max){ //cerr<<req_max<<' '<<req_pos<<nl; res++; people[req_pos]++; }else{ break; } } cout<<res<<nl; } int main() { cin.tie(0)->sync_with_stdio(0); int t; cin>>t; while(t--){ solve(); } return 0; } |
English