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 int long long
#define sum first
#define ind second
using namespace std;
const int N=500005;
int a[N],sums[N], oldS, n;
int ans[N];
priority_queue<pair<int, int> , vector<pair<int, int>> , greater<pair<int, int>>> pq;
main(){
cin>>n;
for(int i=0; i<n; i++){
cin>>a[i];
}
sums[0]=a[0];
for(int i=1; i<n; i++){
sums[i]=a[i]+sums[i-1];
}
int sp=0; // starting point of segment
for(int i=0; i<n; i++){
while(pq.size()!=0 && pq.top().sum <= sums[i]){
int j=pq.top().ind;
if(ans[j]>ans[sp]) sp=j;
if(ans[j]==ans[sp]) sp=max(j, sp);
pq.pop();
}
ans[i] = ans[sp]+1;
pq.push(make_pair(2*sums[i]-sums[sp-1], i));
}
cout<<ans[n-1]<<endl;
}
Compilation message (stderr)
segments.cpp:10:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
10 | main(){
| ^
# | 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... |