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>
#define MAXN 500010
using namespace std;
long long a[MAXN];
long long pre[1000000];
int n;
int binary(int l,int goal)
{
int x=l;
for(int b=(n-x);b>0;b/=2)
{
while((x+b)<=n && pre[x+b]-pre[l]<goal)
{
x+=b;
}
}
x++;
return x;
}
int dfs(int curr, long long sum, int cntr)
{
// for(int i=0;i<cntr;i++) printf(" ");
//printf("in--> cntr(%d)=idx(%d), sum=%d; ",cntr,curr,sum);
if(curr>n) return 0;
// printf("in--> cntr(%d)=idx(%d), sum=%d; \n",cntr,curr,sum);
// printf("in curr=%d,sum=%d,cntr=%d\n ",curr,sum,cntr);
long long nsum=0;
int ans=cntr;
ans=max(ans,dfs(curr+1,sum+a[curr+1],cntr));
int idx=binary(curr,sum);
if(idx>curr)
{
// printf("idx%d = %d ",cntr,idx);
if(pre[idx]-pre[curr]>=sum)ans=max(ans,dfs(idx,pre[idx]-pre[curr],cntr+1));
// ans=max(ans,dfs(idx+1,pre[idx+1]-pre[curr-1],cntr+1));
}
// printf("out\n");
// printf("in curr=%d,sum=%d,cntr=%d -> ans=%d\n",curr,sum,cntr,ans);
return ans;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
pre[i]=pre[i-1]+a[i];
}/*
for(int i=0;i<=n;i++)
{
printf("%d ",pre[i]);
}
printf("\n");*/
cout<<dfs(1,a[1],1)<<"\n";
}
Compilation message (stderr)
segments.cpp: In function 'int dfs(int, long long int, int)':
segments.cpp:27:15: warning: unused variable 'nsum' [-Wunused-variable]
27 | long long nsum=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... |