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 ll long long
#define int long long int
using namespace std;
const int N = (int)100001;
int n;
int a[N], dp[N], ind[N];
ll pref[N];
int t[4*N];
void build(int v,int l,int r){
if(l==r){
t[v]=1e9;
return;
}
int mid=(l+r)>>1;
build(2*v,l,mid);
build(2*v+1,mid+1,r);
t[v]=1e9;
}
void update(int v,int l,int r,int pos,int val){
if(l==pos&&r==pos){
t[v]=val;
return;
}
int mid=(l+r)>>1;
if(pos<=mid)update(2*v,l,mid,pos,val);
else update(2*v+1,mid+1,r,pos,val);
t[v]=min(t[v*2],t[v*2+1]);
}
int get(int v,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr)return t[v];
if(r<ql||qr<l)return 1e9;
int mid=(l+r)>>1;
return min(get(v*2,l,mid,ql,qr),get(v*2+1,mid+1,r,ql,qr));
}
main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pref[i] = pref[i - 1] + a[i];
}
build(1,1,n);
for (int i = 1; i <= n; i++) {
dp[i] = dp[i - 1];
ind[i] = ind[i - 1];
int l = 1,r=i-1,mid;
while(l<r){
mid=(l+r+1)>>1;
if(pref[i]>=get(1,1,n,mid,i-1))l=mid;
else r=mid-1;
}
if(pref[i]<2*pref[l]-pref[ind[l]])l=0;
/*for (int j = 0; j < i; j++) {
if (pref[i] >= 2 * pref[j] - pref[ind[j]]) {
l = j;
}
}*/
update(1,1,n,i,2*pref[i]-pref[l]);
dp[i] = dp[l] + 1;
ind[i] = l;
}
// cerr << ind[4] << endl;
cout << dp[n];
}
Compilation message (stderr)
segments.cpp:39:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main() {
^
segments.cpp: In function 'int main()':
segments.cpp:40:16: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
scanf("%d", &n);
~~^
segments.cpp:42:20: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
scanf("%d", &a[i]);
~~~~~^
segments.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
segments.cpp:42:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
# | 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... |