#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,0,n);
update(1,0,n,0,0);
for (int i = 1; i <= n; i++) {
dp[i] = dp[i - 1];
ind[i] = ind[i - 1];
int l = 0,r=i-1,mid;
while(l<r){
mid=(l+r+1)>>1;
if(pref[i]>=get(1,0,n,mid,i-1))l=mid;
else r=mid-1;
}
for (int j = 0; j < i; j++) {
if (pref[i] >= 2 * pref[j] - pref[ind[j]]) {
l = j;
}
}
update(1,0,n,i,2*pref[i]-pref[l]);
dp[i] = dp[l] + 1;
ind[i] = l;
}
// cerr << ind[4] << endl;
cout << dp[n];
}
Compilation message
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]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
376 KB |
Output is correct |
20 |
Correct |
3 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
380 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
380 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
3 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
376 KB |
Output is correct |
20 |
Correct |
3 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
380 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
380 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
3 ms |
376 KB |
Output is correct |
31 |
Correct |
12 ms |
504 KB |
Output is correct |
32 |
Correct |
13 ms |
504 KB |
Output is correct |
33 |
Correct |
12 ms |
504 KB |
Output is correct |
34 |
Correct |
12 ms |
504 KB |
Output is correct |
35 |
Correct |
12 ms |
504 KB |
Output is correct |
36 |
Correct |
13 ms |
504 KB |
Output is correct |
37 |
Correct |
10 ms |
504 KB |
Output is correct |
38 |
Correct |
8 ms |
504 KB |
Output is correct |
39 |
Correct |
12 ms |
504 KB |
Output is correct |
40 |
Correct |
13 ms |
504 KB |
Output is correct |
41 |
Correct |
12 ms |
632 KB |
Output is correct |
42 |
Correct |
12 ms |
504 KB |
Output is correct |
43 |
Correct |
12 ms |
504 KB |
Output is correct |
44 |
Correct |
12 ms |
504 KB |
Output is correct |
45 |
Correct |
12 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
376 KB |
Output is correct |
20 |
Correct |
3 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
380 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
380 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
3 ms |
376 KB |
Output is correct |
31 |
Correct |
12 ms |
504 KB |
Output is correct |
32 |
Correct |
13 ms |
504 KB |
Output is correct |
33 |
Correct |
12 ms |
504 KB |
Output is correct |
34 |
Correct |
12 ms |
504 KB |
Output is correct |
35 |
Correct |
12 ms |
504 KB |
Output is correct |
36 |
Correct |
13 ms |
504 KB |
Output is correct |
37 |
Correct |
10 ms |
504 KB |
Output is correct |
38 |
Correct |
8 ms |
504 KB |
Output is correct |
39 |
Correct |
12 ms |
504 KB |
Output is correct |
40 |
Correct |
13 ms |
504 KB |
Output is correct |
41 |
Correct |
12 ms |
632 KB |
Output is correct |
42 |
Correct |
12 ms |
504 KB |
Output is correct |
43 |
Correct |
12 ms |
504 KB |
Output is correct |
44 |
Correct |
12 ms |
504 KB |
Output is correct |
45 |
Correct |
12 ms |
504 KB |
Output is correct |
46 |
Execution timed out |
1573 ms |
5176 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
376 KB |
Output is correct |
19 |
Correct |
3 ms |
376 KB |
Output is correct |
20 |
Correct |
3 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
380 KB |
Output is correct |
22 |
Correct |
3 ms |
376 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
3 ms |
376 KB |
Output is correct |
25 |
Correct |
3 ms |
376 KB |
Output is correct |
26 |
Correct |
3 ms |
380 KB |
Output is correct |
27 |
Correct |
3 ms |
376 KB |
Output is correct |
28 |
Correct |
3 ms |
376 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
3 ms |
376 KB |
Output is correct |
31 |
Correct |
12 ms |
504 KB |
Output is correct |
32 |
Correct |
13 ms |
504 KB |
Output is correct |
33 |
Correct |
12 ms |
504 KB |
Output is correct |
34 |
Correct |
12 ms |
504 KB |
Output is correct |
35 |
Correct |
12 ms |
504 KB |
Output is correct |
36 |
Correct |
13 ms |
504 KB |
Output is correct |
37 |
Correct |
10 ms |
504 KB |
Output is correct |
38 |
Correct |
8 ms |
504 KB |
Output is correct |
39 |
Correct |
12 ms |
504 KB |
Output is correct |
40 |
Correct |
13 ms |
504 KB |
Output is correct |
41 |
Correct |
12 ms |
632 KB |
Output is correct |
42 |
Correct |
12 ms |
504 KB |
Output is correct |
43 |
Correct |
12 ms |
504 KB |
Output is correct |
44 |
Correct |
12 ms |
504 KB |
Output is correct |
45 |
Correct |
12 ms |
504 KB |
Output is correct |
46 |
Execution timed out |
1573 ms |
5176 KB |
Time limit exceeded |
47 |
Halted |
0 ms |
0 KB |
- |