#include<bits/stdc++.h>
#define int long long
using namespace std;
const int inf=1e16;
struct Store{
int mod;
deque<int> q;
int get_first(){
int best=q.back()+mod;
if(best<0)return -1;
int mn=0,mx=q.size()-1;
while(mn<mx){
int av=(mn+mx)/2;
if(q[av]+mod < 0){
mn=av+1;
}
else{
mx=av;
}
}
return mn;
}
void push_add(int zero,int add){
q.push_front(-inf);
if(zero!=-1){
q[zero]=-mod;
}
mod+=add;
}
Store(){
mod=0;
q.push_back(0);
}
}store;
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >>n;
for(int i=0;i<n;i++){
int x;
cin >>x;
int y=store.get_first();
store.push_add(y,x);
}
cout <<store.get_first() <<endl;
}
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 | #include<bits/stdc++.h> #define int long long using namespace std; const int inf=1e16; struct Store{ int mod; deque<int> q; int get_first(){ int best=q.back()+mod; if(best<0)return -1; int mn=0,mx=q.size()-1; while(mn<mx){ int av=(mn+mx)/2; if(q[av]+mod < 0){ mn=av+1; } else{ mx=av; } } return mn; } void push_add(int zero,int add){ q.push_front(-inf); if(zero!=-1){ q[zero]=-mod; } mod+=add; } Store(){ mod=0; q.push_back(0); } }store; int32_t main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >>n; for(int i=0;i<n;i++){ int x; cin >>x; int y=store.get_first(); store.push_add(y,x); } cout <<store.get_first() <<endl; } |
English