#include "bits/stdc++.h"
using namespace std;
const int N = 5e5 + 5;
int a[N];
int dp[N];
long long pref[N];
long long seg[N * 2];
void upd(int nd, int l, int r, int p, long long v) {
if (l == r) {
seg[nd] = v;
return;
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
if (p <= mid) {
upd(nd + 1, l, mid, p, v);
} else {
upd(rs, mid + 1, r, p, v);
}
seg[nd] = min(seg[nd + 1], seg[rs]);
}
int get(int nd, int l, int r, int s, int e, long long v) {
if (l > e || r < s || seg[nd] > v) {
return -1;
}
if (l == r) {
return l;
}
int mid = (l + r) >> 1;
int rs = nd + ((mid - l + 1) << 1);
int ret = get(rs, mid + 1, r, s, e, v);
if (ret == -1) {
ret = get(nd + 1, l, mid, s, e, v);
}
return ret;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
fill(seg, seg + N * 2, 1e18);
upd(0, 0, N, 0, 0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
dp[1] = 1;
upd(0, 0, N, 1, a[1] * 2);
for (int i = 2; i <= n; ++i) {
int j = get(0, 0, N, 0, i - 1, pref[i]);
dp[i] = dp[j] + 1;
long long sum = pref[i] - pref[j];
upd(0, 0, N, i, sum + pref[i]);
}
cout << dp[n] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12888 KB |
Output is correct |
5 |
Correct |
3 ms |
12888 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12888 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12912 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12888 KB |
Output is correct |
5 |
Correct |
3 ms |
12888 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12888 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12912 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
2 ms |
12892 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
2 ms |
12892 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
23 |
Correct |
2 ms |
12928 KB |
Output is correct |
24 |
Correct |
2 ms |
12892 KB |
Output is correct |
25 |
Correct |
2 ms |
12892 KB |
Output is correct |
26 |
Correct |
2 ms |
12888 KB |
Output is correct |
27 |
Correct |
2 ms |
12892 KB |
Output is correct |
28 |
Correct |
2 ms |
12892 KB |
Output is correct |
29 |
Correct |
3 ms |
12892 KB |
Output is correct |
30 |
Correct |
2 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12888 KB |
Output is correct |
5 |
Correct |
3 ms |
12888 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12888 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12912 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
2 ms |
12892 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
2 ms |
12892 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
23 |
Correct |
2 ms |
12928 KB |
Output is correct |
24 |
Correct |
2 ms |
12892 KB |
Output is correct |
25 |
Correct |
2 ms |
12892 KB |
Output is correct |
26 |
Correct |
2 ms |
12888 KB |
Output is correct |
27 |
Correct |
2 ms |
12892 KB |
Output is correct |
28 |
Correct |
2 ms |
12892 KB |
Output is correct |
29 |
Correct |
3 ms |
12892 KB |
Output is correct |
30 |
Correct |
2 ms |
12892 KB |
Output is correct |
31 |
Correct |
3 ms |
12892 KB |
Output is correct |
32 |
Correct |
3 ms |
12892 KB |
Output is correct |
33 |
Correct |
3 ms |
12892 KB |
Output is correct |
34 |
Correct |
3 ms |
12892 KB |
Output is correct |
35 |
Correct |
3 ms |
12892 KB |
Output is correct |
36 |
Correct |
3 ms |
12892 KB |
Output is correct |
37 |
Correct |
2 ms |
12892 KB |
Output is correct |
38 |
Correct |
3 ms |
12892 KB |
Output is correct |
39 |
Correct |
3 ms |
12888 KB |
Output is correct |
40 |
Correct |
3 ms |
12908 KB |
Output is correct |
41 |
Correct |
3 ms |
12892 KB |
Output is correct |
42 |
Correct |
3 ms |
12892 KB |
Output is correct |
43 |
Correct |
2 ms |
12892 KB |
Output is correct |
44 |
Correct |
3 ms |
12892 KB |
Output is correct |
45 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12888 KB |
Output is correct |
5 |
Correct |
3 ms |
12888 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12888 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12912 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
2 ms |
12892 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
2 ms |
12892 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
23 |
Correct |
2 ms |
12928 KB |
Output is correct |
24 |
Correct |
2 ms |
12892 KB |
Output is correct |
25 |
Correct |
2 ms |
12892 KB |
Output is correct |
26 |
Correct |
2 ms |
12888 KB |
Output is correct |
27 |
Correct |
2 ms |
12892 KB |
Output is correct |
28 |
Correct |
2 ms |
12892 KB |
Output is correct |
29 |
Correct |
3 ms |
12892 KB |
Output is correct |
30 |
Correct |
2 ms |
12892 KB |
Output is correct |
31 |
Correct |
3 ms |
12892 KB |
Output is correct |
32 |
Correct |
3 ms |
12892 KB |
Output is correct |
33 |
Correct |
3 ms |
12892 KB |
Output is correct |
34 |
Correct |
3 ms |
12892 KB |
Output is correct |
35 |
Correct |
3 ms |
12892 KB |
Output is correct |
36 |
Correct |
3 ms |
12892 KB |
Output is correct |
37 |
Correct |
2 ms |
12892 KB |
Output is correct |
38 |
Correct |
3 ms |
12892 KB |
Output is correct |
39 |
Correct |
3 ms |
12888 KB |
Output is correct |
40 |
Correct |
3 ms |
12908 KB |
Output is correct |
41 |
Correct |
3 ms |
12892 KB |
Output is correct |
42 |
Correct |
3 ms |
12892 KB |
Output is correct |
43 |
Correct |
2 ms |
12892 KB |
Output is correct |
44 |
Correct |
3 ms |
12892 KB |
Output is correct |
45 |
Correct |
3 ms |
12892 KB |
Output is correct |
46 |
Correct |
23 ms |
14936 KB |
Output is correct |
47 |
Correct |
27 ms |
15452 KB |
Output is correct |
48 |
Correct |
26 ms |
15704 KB |
Output is correct |
49 |
Correct |
28 ms |
15708 KB |
Output is correct |
50 |
Correct |
30 ms |
15964 KB |
Output is correct |
51 |
Correct |
29 ms |
15964 KB |
Output is correct |
52 |
Correct |
15 ms |
13400 KB |
Output is correct |
53 |
Correct |
10 ms |
13148 KB |
Output is correct |
54 |
Correct |
27 ms |
15196 KB |
Output is correct |
55 |
Correct |
32 ms |
16004 KB |
Output is correct |
56 |
Correct |
30 ms |
15456 KB |
Output is correct |
57 |
Correct |
27 ms |
15196 KB |
Output is correct |
58 |
Correct |
26 ms |
15448 KB |
Output is correct |
59 |
Correct |
27 ms |
15452 KB |
Output is correct |
60 |
Correct |
29 ms |
15508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12888 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12888 KB |
Output is correct |
5 |
Correct |
3 ms |
12888 KB |
Output is correct |
6 |
Correct |
2 ms |
12892 KB |
Output is correct |
7 |
Correct |
2 ms |
12892 KB |
Output is correct |
8 |
Correct |
2 ms |
12888 KB |
Output is correct |
9 |
Correct |
2 ms |
12888 KB |
Output is correct |
10 |
Correct |
2 ms |
12892 KB |
Output is correct |
11 |
Correct |
2 ms |
12912 KB |
Output is correct |
12 |
Correct |
2 ms |
12892 KB |
Output is correct |
13 |
Correct |
2 ms |
12892 KB |
Output is correct |
14 |
Correct |
2 ms |
12892 KB |
Output is correct |
15 |
Correct |
2 ms |
12892 KB |
Output is correct |
16 |
Correct |
3 ms |
12892 KB |
Output is correct |
17 |
Correct |
2 ms |
12892 KB |
Output is correct |
18 |
Correct |
2 ms |
12892 KB |
Output is correct |
19 |
Correct |
2 ms |
12892 KB |
Output is correct |
20 |
Correct |
2 ms |
12892 KB |
Output is correct |
21 |
Correct |
2 ms |
12892 KB |
Output is correct |
22 |
Correct |
2 ms |
12892 KB |
Output is correct |
23 |
Correct |
2 ms |
12928 KB |
Output is correct |
24 |
Correct |
2 ms |
12892 KB |
Output is correct |
25 |
Correct |
2 ms |
12892 KB |
Output is correct |
26 |
Correct |
2 ms |
12888 KB |
Output is correct |
27 |
Correct |
2 ms |
12892 KB |
Output is correct |
28 |
Correct |
2 ms |
12892 KB |
Output is correct |
29 |
Correct |
3 ms |
12892 KB |
Output is correct |
30 |
Correct |
2 ms |
12892 KB |
Output is correct |
31 |
Correct |
3 ms |
12892 KB |
Output is correct |
32 |
Correct |
3 ms |
12892 KB |
Output is correct |
33 |
Correct |
3 ms |
12892 KB |
Output is correct |
34 |
Correct |
3 ms |
12892 KB |
Output is correct |
35 |
Correct |
3 ms |
12892 KB |
Output is correct |
36 |
Correct |
3 ms |
12892 KB |
Output is correct |
37 |
Correct |
2 ms |
12892 KB |
Output is correct |
38 |
Correct |
3 ms |
12892 KB |
Output is correct |
39 |
Correct |
3 ms |
12888 KB |
Output is correct |
40 |
Correct |
3 ms |
12908 KB |
Output is correct |
41 |
Correct |
3 ms |
12892 KB |
Output is correct |
42 |
Correct |
3 ms |
12892 KB |
Output is correct |
43 |
Correct |
2 ms |
12892 KB |
Output is correct |
44 |
Correct |
3 ms |
12892 KB |
Output is correct |
45 |
Correct |
3 ms |
12892 KB |
Output is correct |
46 |
Correct |
23 ms |
14936 KB |
Output is correct |
47 |
Correct |
27 ms |
15452 KB |
Output is correct |
48 |
Correct |
26 ms |
15704 KB |
Output is correct |
49 |
Correct |
28 ms |
15708 KB |
Output is correct |
50 |
Correct |
30 ms |
15964 KB |
Output is correct |
51 |
Correct |
29 ms |
15964 KB |
Output is correct |
52 |
Correct |
15 ms |
13400 KB |
Output is correct |
53 |
Correct |
10 ms |
13148 KB |
Output is correct |
54 |
Correct |
27 ms |
15196 KB |
Output is correct |
55 |
Correct |
32 ms |
16004 KB |
Output is correct |
56 |
Correct |
30 ms |
15456 KB |
Output is correct |
57 |
Correct |
27 ms |
15196 KB |
Output is correct |
58 |
Correct |
26 ms |
15448 KB |
Output is correct |
59 |
Correct |
27 ms |
15452 KB |
Output is correct |
60 |
Correct |
29 ms |
15508 KB |
Output is correct |
61 |
Correct |
112 ms |
18004 KB |
Output is correct |
62 |
Correct |
119 ms |
17816 KB |
Output is correct |
63 |
Correct |
118 ms |
18268 KB |
Output is correct |
64 |
Correct |
137 ms |
20632 KB |
Output is correct |
65 |
Correct |
153 ms |
21180 KB |
Output is correct |
66 |
Correct |
146 ms |
20920 KB |
Output is correct |
67 |
Correct |
114 ms |
19456 KB |
Output is correct |
68 |
Correct |
61 ms |
16916 KB |
Output is correct |
69 |
Correct |
126 ms |
16888 KB |
Output is correct |
70 |
Correct |
140 ms |
20820 KB |
Output is correct |
71 |
Correct |
125 ms |
18040 KB |
Output is correct |
72 |
Correct |
117 ms |
18256 KB |
Output is correct |
73 |
Correct |
129 ms |
19024 KB |
Output is correct |
74 |
Correct |
130 ms |
19632 KB |
Output is correct |
75 |
Correct |
132 ms |
19440 KB |
Output is correct |