/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 1e6+100, M = 1e5+10, K = 52, MX = 30;
int n;
ll a[N], dp[N][2], pref[N];
vector<int> dps[N];
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
dp[0][0] = 0;
dp[0][1] = 0;
pref[0] = 0;
for(int i = 1; i <= n; ++i) pref[i] = pref[i - 1] + a[i], dps[i].pb(0);
for(int i = 1; i <= n; ++i){
dp[i][0] = 0;
dp[i][0] = dp[i - 1][0];
dp[i][1] = dp[i - 1][1] + a[i];
for(int j: dps[i]){
ll s = pref[i] - pref[j];
if(s >= dp[j][1]){
if(dp[i][0] < dp[j][0] + 1){
dp[i][0] = dp[j][0] + 1;
dp[i][1] = s;
}else if(dp[i][0] == dp[j][0] + 1) dp[i][1] = min(dp[i][1], s);
}
}
int l = i + 1, r = n, cc = n + 1;
while(l <= r){
int m = l+r>>1;
if(pref[m] - pref[i] >= dp[i][1]){
cc = m;
r = m - 1;
}else l = m + 1;
}
dps[cc].pb(i);
}
cout << dp[n][0];
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
segments.cpp: In function 'void solve()':
segments.cpp:38:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int m = l+r>>1;
| ~^~
segments.cpp: In function 'int main()':
segments.cpp:52:15: warning: unused variable 'aa' [-Wunused-variable]
52 | int tt = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29276 KB |
Output is correct |
3 |
Correct |
7 ms |
29348 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
7 ms |
29316 KB |
Output is correct |
6 |
Correct |
6 ms |
29528 KB |
Output is correct |
7 |
Correct |
6 ms |
29308 KB |
Output is correct |
8 |
Correct |
6 ms |
29272 KB |
Output is correct |
9 |
Correct |
6 ms |
29276 KB |
Output is correct |
10 |
Correct |
6 ms |
29360 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29308 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29276 KB |
Output is correct |
3 |
Correct |
7 ms |
29348 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
7 ms |
29316 KB |
Output is correct |
6 |
Correct |
6 ms |
29528 KB |
Output is correct |
7 |
Correct |
6 ms |
29308 KB |
Output is correct |
8 |
Correct |
6 ms |
29272 KB |
Output is correct |
9 |
Correct |
6 ms |
29276 KB |
Output is correct |
10 |
Correct |
6 ms |
29360 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29308 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29528 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29272 KB |
Output is correct |
19 |
Correct |
6 ms |
29276 KB |
Output is correct |
20 |
Correct |
6 ms |
29276 KB |
Output is correct |
21 |
Correct |
6 ms |
29276 KB |
Output is correct |
22 |
Correct |
7 ms |
29276 KB |
Output is correct |
23 |
Correct |
6 ms |
29276 KB |
Output is correct |
24 |
Correct |
6 ms |
29276 KB |
Output is correct |
25 |
Correct |
6 ms |
29276 KB |
Output is correct |
26 |
Correct |
6 ms |
29284 KB |
Output is correct |
27 |
Correct |
6 ms |
29276 KB |
Output is correct |
28 |
Correct |
6 ms |
29276 KB |
Output is correct |
29 |
Correct |
6 ms |
29276 KB |
Output is correct |
30 |
Correct |
6 ms |
29316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29276 KB |
Output is correct |
3 |
Correct |
7 ms |
29348 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
7 ms |
29316 KB |
Output is correct |
6 |
Correct |
6 ms |
29528 KB |
Output is correct |
7 |
Correct |
6 ms |
29308 KB |
Output is correct |
8 |
Correct |
6 ms |
29272 KB |
Output is correct |
9 |
Correct |
6 ms |
29276 KB |
Output is correct |
10 |
Correct |
6 ms |
29360 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29308 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29528 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29272 KB |
Output is correct |
19 |
Correct |
6 ms |
29276 KB |
Output is correct |
20 |
Correct |
6 ms |
29276 KB |
Output is correct |
21 |
Correct |
6 ms |
29276 KB |
Output is correct |
22 |
Correct |
7 ms |
29276 KB |
Output is correct |
23 |
Correct |
6 ms |
29276 KB |
Output is correct |
24 |
Correct |
6 ms |
29276 KB |
Output is correct |
25 |
Correct |
6 ms |
29276 KB |
Output is correct |
26 |
Correct |
6 ms |
29284 KB |
Output is correct |
27 |
Correct |
6 ms |
29276 KB |
Output is correct |
28 |
Correct |
6 ms |
29276 KB |
Output is correct |
29 |
Correct |
6 ms |
29276 KB |
Output is correct |
30 |
Correct |
6 ms |
29316 KB |
Output is correct |
31 |
Correct |
8 ms |
29272 KB |
Output is correct |
32 |
Correct |
6 ms |
29272 KB |
Output is correct |
33 |
Correct |
8 ms |
29276 KB |
Output is correct |
34 |
Correct |
6 ms |
29300 KB |
Output is correct |
35 |
Correct |
6 ms |
29276 KB |
Output is correct |
36 |
Correct |
7 ms |
29276 KB |
Output is correct |
37 |
Correct |
6 ms |
29288 KB |
Output is correct |
38 |
Correct |
6 ms |
29276 KB |
Output is correct |
39 |
Correct |
6 ms |
29528 KB |
Output is correct |
40 |
Correct |
6 ms |
29276 KB |
Output is correct |
41 |
Correct |
6 ms |
29528 KB |
Output is correct |
42 |
Correct |
6 ms |
29276 KB |
Output is correct |
43 |
Correct |
6 ms |
29276 KB |
Output is correct |
44 |
Correct |
6 ms |
29276 KB |
Output is correct |
45 |
Correct |
7 ms |
29276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29276 KB |
Output is correct |
3 |
Correct |
7 ms |
29348 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
7 ms |
29316 KB |
Output is correct |
6 |
Correct |
6 ms |
29528 KB |
Output is correct |
7 |
Correct |
6 ms |
29308 KB |
Output is correct |
8 |
Correct |
6 ms |
29272 KB |
Output is correct |
9 |
Correct |
6 ms |
29276 KB |
Output is correct |
10 |
Correct |
6 ms |
29360 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29308 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29528 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29272 KB |
Output is correct |
19 |
Correct |
6 ms |
29276 KB |
Output is correct |
20 |
Correct |
6 ms |
29276 KB |
Output is correct |
21 |
Correct |
6 ms |
29276 KB |
Output is correct |
22 |
Correct |
7 ms |
29276 KB |
Output is correct |
23 |
Correct |
6 ms |
29276 KB |
Output is correct |
24 |
Correct |
6 ms |
29276 KB |
Output is correct |
25 |
Correct |
6 ms |
29276 KB |
Output is correct |
26 |
Correct |
6 ms |
29284 KB |
Output is correct |
27 |
Correct |
6 ms |
29276 KB |
Output is correct |
28 |
Correct |
6 ms |
29276 KB |
Output is correct |
29 |
Correct |
6 ms |
29276 KB |
Output is correct |
30 |
Correct |
6 ms |
29316 KB |
Output is correct |
31 |
Correct |
8 ms |
29272 KB |
Output is correct |
32 |
Correct |
6 ms |
29272 KB |
Output is correct |
33 |
Correct |
8 ms |
29276 KB |
Output is correct |
34 |
Correct |
6 ms |
29300 KB |
Output is correct |
35 |
Correct |
6 ms |
29276 KB |
Output is correct |
36 |
Correct |
7 ms |
29276 KB |
Output is correct |
37 |
Correct |
6 ms |
29288 KB |
Output is correct |
38 |
Correct |
6 ms |
29276 KB |
Output is correct |
39 |
Correct |
6 ms |
29528 KB |
Output is correct |
40 |
Correct |
6 ms |
29276 KB |
Output is correct |
41 |
Correct |
6 ms |
29528 KB |
Output is correct |
42 |
Correct |
6 ms |
29276 KB |
Output is correct |
43 |
Correct |
6 ms |
29276 KB |
Output is correct |
44 |
Correct |
6 ms |
29276 KB |
Output is correct |
45 |
Correct |
7 ms |
29276 KB |
Output is correct |
46 |
Correct |
20 ms |
35032 KB |
Output is correct |
47 |
Correct |
21 ms |
34648 KB |
Output is correct |
48 |
Correct |
23 ms |
34652 KB |
Output is correct |
49 |
Correct |
20 ms |
34652 KB |
Output is correct |
50 |
Correct |
20 ms |
34480 KB |
Output is correct |
51 |
Correct |
24 ms |
34640 KB |
Output is correct |
52 |
Correct |
15 ms |
30812 KB |
Output is correct |
53 |
Correct |
11 ms |
30300 KB |
Output is correct |
54 |
Correct |
21 ms |
34756 KB |
Output is correct |
55 |
Correct |
22 ms |
34396 KB |
Output is correct |
56 |
Correct |
22 ms |
34396 KB |
Output is correct |
57 |
Correct |
18 ms |
34648 KB |
Output is correct |
58 |
Correct |
19 ms |
34652 KB |
Output is correct |
59 |
Correct |
19 ms |
34396 KB |
Output is correct |
60 |
Correct |
19 ms |
34396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
29276 KB |
Output is correct |
2 |
Correct |
7 ms |
29276 KB |
Output is correct |
3 |
Correct |
7 ms |
29348 KB |
Output is correct |
4 |
Correct |
6 ms |
29276 KB |
Output is correct |
5 |
Correct |
7 ms |
29316 KB |
Output is correct |
6 |
Correct |
6 ms |
29528 KB |
Output is correct |
7 |
Correct |
6 ms |
29308 KB |
Output is correct |
8 |
Correct |
6 ms |
29272 KB |
Output is correct |
9 |
Correct |
6 ms |
29276 KB |
Output is correct |
10 |
Correct |
6 ms |
29360 KB |
Output is correct |
11 |
Correct |
6 ms |
29276 KB |
Output is correct |
12 |
Correct |
6 ms |
29276 KB |
Output is correct |
13 |
Correct |
6 ms |
29276 KB |
Output is correct |
14 |
Correct |
7 ms |
29308 KB |
Output is correct |
15 |
Correct |
6 ms |
29276 KB |
Output is correct |
16 |
Correct |
6 ms |
29528 KB |
Output is correct |
17 |
Correct |
6 ms |
29276 KB |
Output is correct |
18 |
Correct |
6 ms |
29272 KB |
Output is correct |
19 |
Correct |
6 ms |
29276 KB |
Output is correct |
20 |
Correct |
6 ms |
29276 KB |
Output is correct |
21 |
Correct |
6 ms |
29276 KB |
Output is correct |
22 |
Correct |
7 ms |
29276 KB |
Output is correct |
23 |
Correct |
6 ms |
29276 KB |
Output is correct |
24 |
Correct |
6 ms |
29276 KB |
Output is correct |
25 |
Correct |
6 ms |
29276 KB |
Output is correct |
26 |
Correct |
6 ms |
29284 KB |
Output is correct |
27 |
Correct |
6 ms |
29276 KB |
Output is correct |
28 |
Correct |
6 ms |
29276 KB |
Output is correct |
29 |
Correct |
6 ms |
29276 KB |
Output is correct |
30 |
Correct |
6 ms |
29316 KB |
Output is correct |
31 |
Correct |
8 ms |
29272 KB |
Output is correct |
32 |
Correct |
6 ms |
29272 KB |
Output is correct |
33 |
Correct |
8 ms |
29276 KB |
Output is correct |
34 |
Correct |
6 ms |
29300 KB |
Output is correct |
35 |
Correct |
6 ms |
29276 KB |
Output is correct |
36 |
Correct |
7 ms |
29276 KB |
Output is correct |
37 |
Correct |
6 ms |
29288 KB |
Output is correct |
38 |
Correct |
6 ms |
29276 KB |
Output is correct |
39 |
Correct |
6 ms |
29528 KB |
Output is correct |
40 |
Correct |
6 ms |
29276 KB |
Output is correct |
41 |
Correct |
6 ms |
29528 KB |
Output is correct |
42 |
Correct |
6 ms |
29276 KB |
Output is correct |
43 |
Correct |
6 ms |
29276 KB |
Output is correct |
44 |
Correct |
6 ms |
29276 KB |
Output is correct |
45 |
Correct |
7 ms |
29276 KB |
Output is correct |
46 |
Correct |
20 ms |
35032 KB |
Output is correct |
47 |
Correct |
21 ms |
34648 KB |
Output is correct |
48 |
Correct |
23 ms |
34652 KB |
Output is correct |
49 |
Correct |
20 ms |
34652 KB |
Output is correct |
50 |
Correct |
20 ms |
34480 KB |
Output is correct |
51 |
Correct |
24 ms |
34640 KB |
Output is correct |
52 |
Correct |
15 ms |
30812 KB |
Output is correct |
53 |
Correct |
11 ms |
30300 KB |
Output is correct |
54 |
Correct |
21 ms |
34756 KB |
Output is correct |
55 |
Correct |
22 ms |
34396 KB |
Output is correct |
56 |
Correct |
22 ms |
34396 KB |
Output is correct |
57 |
Correct |
18 ms |
34648 KB |
Output is correct |
58 |
Correct |
19 ms |
34652 KB |
Output is correct |
59 |
Correct |
19 ms |
34396 KB |
Output is correct |
60 |
Correct |
19 ms |
34396 KB |
Output is correct |
61 |
Correct |
59 ms |
61408 KB |
Output is correct |
62 |
Correct |
76 ms |
61640 KB |
Output is correct |
63 |
Correct |
88 ms |
60760 KB |
Output is correct |
64 |
Correct |
86 ms |
59292 KB |
Output is correct |
65 |
Correct |
82 ms |
59316 KB |
Output is correct |
66 |
Correct |
95 ms |
59820 KB |
Output is correct |
67 |
Correct |
77 ms |
54612 KB |
Output is correct |
68 |
Correct |
41 ms |
41820 KB |
Output is correct |
69 |
Correct |
81 ms |
60588 KB |
Output is correct |
70 |
Correct |
84 ms |
59216 KB |
Output is correct |
71 |
Correct |
66 ms |
59216 KB |
Output is correct |
72 |
Correct |
70 ms |
60236 KB |
Output is correct |
73 |
Correct |
71 ms |
59592 KB |
Output is correct |
74 |
Correct |
71 ms |
59220 KB |
Output is correct |
75 |
Correct |
74 ms |
59312 KB |
Output is correct |