#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define f first
#define s second
#define SZ(x) (ll)x.size()
#define ALL(x) x.begin(),x.end()
#define lb lower_bound
#define ub upper_bound
const ll MAXN=200001;
const ll MOD=1e9+7;
const ll INF = 3e9;
ll A[MAXN];
ll N;
ll dp[MAXN];
ll S[MAXN];
struct node{
ll s,e,m;
node *l,*r;
pi v;
node(ll _s,ll _e):s(_s),e(_e){
m=(s+e)/2;
v=mp(0,0);
l=r=nullptr;
}
pi ask(ll x,ll y){
if(s==x&&e==y)return v;
pi t=mp(0,0);
if(l&&x<=m)t=max(t, l->ask(x,min(y,m)));
if(r&&y>m)t=max(t,r->ask(max(m+1,x),y));
return t;
}
void up(ll x,pi va){
if(s==e){v=max(v,va);return;}
if(x<=m){
if(!l)l=new node(s,m);
l->up(x,va);
}else{
if(!r)r=new node(m+1,e);
r->up(x,va);
}
v=mp(0,0);
if(l)v=max(v,l->v);
if(r)v=max(v,r->v);
}
}*root;
int main(){
cin>>N;
for(int i=1;i<=N;++i){
cin>>A[i];
A[i]+=A[i-1];
}
root=new node(1,A[N]);
dp[0]=0;
for(int i=1;i<=N;++i){
pi x=root->ask(0,A[i]);
// cerr<<x.f<<' '<<x.s<<'\n';
ll p=abs(x.s);
dp[i]=x.f+1;
root->up(2*A[i]-A[p], mp(dp[i],-i));
// for(int j=1;j<=i;++j){
// // take from i to j
// // cerr<<i<<' '<<j<<' '<<x<<'\n';
// if(x >= S[j-1]){
// if(dp[j-1]+1 >= dp[i]){
// dp[i]=dp[j-1]+1;
// S[i] = x+A[i]-A[j-1];
// }
// }
// }
// cerr<<i<<' '<<dp[i]<<' '<<S[i]<<'\n';
}
cout<<dp[N];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |