This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Q=1<<19;
ll tree[2*Q];
ll inf;
void update(int p, ll x) {
p+=Q;
tree[p]=x;
p/=2;
while(p>0){
tree[p]=min(tree[2*p],tree[2*p+1]);
p/=2;
}
}
int get(int l, int r) {
ll ans=inf;
for(l+=Q,r+=Q;l<r;l>>=1,r>>=1){
if(l&1)ans=min(ans,tree[l++]);
if(r&1)ans=min(ans,tree[--r]);
}
return ans;
}
int main() {
memset(tree,60,sizeof(tree));
inf=tree[0];
//cout<<inf<<endl;
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin>>n;
vector<int>a(n);
for(int i=0;i<n;i++)cin>>a[i];
vector<ll>pr(n+1);
for(int i=0;i<n;i++)pr[i+1]=pr[i]+a[i];
vector<int>ans(n);
vector<ll> ans_sum(n);
ll sum=0;
for(int i=0;i<n;i++){
sum+=a[i];
ans_sum[i]=sum;
ans[i]=1;
}
update(0,ans_sum[0]+pr[1]);
for(int i=1;i<n;i++){
if(i>=100){
int L=0,R=i;
while(R-L>1){
int M=(L+R)/2;
if(get(M,i)<=pr[i+1]){
L=M;
} else {
R=M;
}
}
if(pr[i+1]>=ans_sum[L]+pr[L+1]){
ans[i]=ans[L]+1;
ans_sum[i]=pr[i+1]-pr[L+1];
}
}else{
for(int j=i-1;j>=0;j--){
if(pr[i+1]>=ans_sum[j]+pr[j+1]){
if(ans[i]<ans[j]+1){
ans[i]=ans[j]+1;
ans_sum[i]=pr[i+1]-pr[j+1];
break;
}
}
}
}
update(i,ans_sum[i]+pr[i+1]);
}
cout<<ans[n-1];
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |