#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef long long ll;
const int maxn = 3e3+10;
const ll inf = 1e18+10;
ll v[maxn];
struct SegmentTree
{
ll tree[4*maxn];
void build(int node, int l, int r)
{
if (l == r)
{
tree[node] = v[l];
return;
}
int mid = (l+r)>>1;
build(2*node, l, mid); build(2*node+1, mid+1, r);
tree[node] = min(tree[2*node], tree[2*node+1]);
}
int query(int node, int l, int r, int pos, ll v)
{
if (l > pos || tree[node] > v) return -1;
if (l == r) return l;
int mid = (l+r)>>1;
int p1 = query(2*node+1, mid+1, r, pos, v);
if (p1 == -1) return query(2*node, l, mid, pos, v);
return p1;
}
} seg;
int a[maxn];
ll pref[maxn];
ll dp[maxn][maxn];
int main(void)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
for (int i = 1; i <= n; i++)
for (int j = 2; j <= n; j++)
dp[i][j] = inf;
for (int i = 1; i <= n; i++)
{
pref[i] = pref[i-1] + 1ll*a[i];
dp[i][1] = pref[i];
v[i] = dp[i][1]+pref[i];
}
for (int j = 2; j <= n; j++)
{
seg.build(1, 1, n);
for (int i = j; i <= n; i++)
{
int p = seg.query(1, 1, n, i-1, pref[i]);
if (p != -1) dp[i][j] = pref[i]-pref[p];
}
for (int i = 1; i <= n; i++)
v[i] = min(inf, dp[i][j]+pref[i]);
}
int ans = 1;
for (int i = 1; i <= n; i++)
if (dp[n][i] != inf)
ans = i;
printf("%d\n", ans);
}
Compilation message
segments.cpp: In function 'int main()':
segments.cpp:57:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
57 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
segments.cpp:60:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
60 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
4352 KB |
Output is correct |
17 |
Correct |
5 ms |
4352 KB |
Output is correct |
18 |
Correct |
7 ms |
4480 KB |
Output is correct |
19 |
Correct |
10 ms |
4352 KB |
Output is correct |
20 |
Correct |
12 ms |
4352 KB |
Output is correct |
21 |
Correct |
6 ms |
4352 KB |
Output is correct |
22 |
Correct |
4 ms |
3200 KB |
Output is correct |
23 |
Correct |
3 ms |
2304 KB |
Output is correct |
24 |
Correct |
7 ms |
4352 KB |
Output is correct |
25 |
Correct |
6 ms |
4352 KB |
Output is correct |
26 |
Correct |
12 ms |
4352 KB |
Output is correct |
27 |
Correct |
8 ms |
4352 KB |
Output is correct |
28 |
Correct |
7 ms |
4352 KB |
Output is correct |
29 |
Correct |
12 ms |
4352 KB |
Output is correct |
30 |
Correct |
11 ms |
4352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
4352 KB |
Output is correct |
17 |
Correct |
5 ms |
4352 KB |
Output is correct |
18 |
Correct |
7 ms |
4480 KB |
Output is correct |
19 |
Correct |
10 ms |
4352 KB |
Output is correct |
20 |
Correct |
12 ms |
4352 KB |
Output is correct |
21 |
Correct |
6 ms |
4352 KB |
Output is correct |
22 |
Correct |
4 ms |
3200 KB |
Output is correct |
23 |
Correct |
3 ms |
2304 KB |
Output is correct |
24 |
Correct |
7 ms |
4352 KB |
Output is correct |
25 |
Correct |
6 ms |
4352 KB |
Output is correct |
26 |
Correct |
12 ms |
4352 KB |
Output is correct |
27 |
Correct |
8 ms |
4352 KB |
Output is correct |
28 |
Correct |
7 ms |
4352 KB |
Output is correct |
29 |
Correct |
12 ms |
4352 KB |
Output is correct |
30 |
Correct |
11 ms |
4352 KB |
Output is correct |
31 |
Correct |
237 ms |
71288 KB |
Output is correct |
32 |
Correct |
238 ms |
71288 KB |
Output is correct |
33 |
Correct |
263 ms |
71160 KB |
Output is correct |
34 |
Correct |
559 ms |
71288 KB |
Output is correct |
35 |
Correct |
692 ms |
71288 KB |
Output is correct |
36 |
Correct |
256 ms |
71344 KB |
Output is correct |
37 |
Correct |
186 ms |
59388 KB |
Output is correct |
38 |
Correct |
120 ms |
39928 KB |
Output is correct |
39 |
Correct |
260 ms |
71160 KB |
Output is correct |
40 |
Correct |
264 ms |
71288 KB |
Output is correct |
41 |
Correct |
699 ms |
71160 KB |
Output is correct |
42 |
Correct |
582 ms |
71288 KB |
Output is correct |
43 |
Correct |
334 ms |
71160 KB |
Output is correct |
44 |
Correct |
709 ms |
71288 KB |
Output is correct |
45 |
Correct |
682 ms |
71288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
4352 KB |
Output is correct |
17 |
Correct |
5 ms |
4352 KB |
Output is correct |
18 |
Correct |
7 ms |
4480 KB |
Output is correct |
19 |
Correct |
10 ms |
4352 KB |
Output is correct |
20 |
Correct |
12 ms |
4352 KB |
Output is correct |
21 |
Correct |
6 ms |
4352 KB |
Output is correct |
22 |
Correct |
4 ms |
3200 KB |
Output is correct |
23 |
Correct |
3 ms |
2304 KB |
Output is correct |
24 |
Correct |
7 ms |
4352 KB |
Output is correct |
25 |
Correct |
6 ms |
4352 KB |
Output is correct |
26 |
Correct |
12 ms |
4352 KB |
Output is correct |
27 |
Correct |
8 ms |
4352 KB |
Output is correct |
28 |
Correct |
7 ms |
4352 KB |
Output is correct |
29 |
Correct |
12 ms |
4352 KB |
Output is correct |
30 |
Correct |
11 ms |
4352 KB |
Output is correct |
31 |
Correct |
237 ms |
71288 KB |
Output is correct |
32 |
Correct |
238 ms |
71288 KB |
Output is correct |
33 |
Correct |
263 ms |
71160 KB |
Output is correct |
34 |
Correct |
559 ms |
71288 KB |
Output is correct |
35 |
Correct |
692 ms |
71288 KB |
Output is correct |
36 |
Correct |
256 ms |
71344 KB |
Output is correct |
37 |
Correct |
186 ms |
59388 KB |
Output is correct |
38 |
Correct |
120 ms |
39928 KB |
Output is correct |
39 |
Correct |
260 ms |
71160 KB |
Output is correct |
40 |
Correct |
264 ms |
71288 KB |
Output is correct |
41 |
Correct |
699 ms |
71160 KB |
Output is correct |
42 |
Correct |
582 ms |
71288 KB |
Output is correct |
43 |
Correct |
334 ms |
71160 KB |
Output is correct |
44 |
Correct |
709 ms |
71288 KB |
Output is correct |
45 |
Correct |
682 ms |
71288 KB |
Output is correct |
46 |
Execution timed out |
5 ms |
768 KB |
Time limit exceeded (wall clock) |
47 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
4352 KB |
Output is correct |
17 |
Correct |
5 ms |
4352 KB |
Output is correct |
18 |
Correct |
7 ms |
4480 KB |
Output is correct |
19 |
Correct |
10 ms |
4352 KB |
Output is correct |
20 |
Correct |
12 ms |
4352 KB |
Output is correct |
21 |
Correct |
6 ms |
4352 KB |
Output is correct |
22 |
Correct |
4 ms |
3200 KB |
Output is correct |
23 |
Correct |
3 ms |
2304 KB |
Output is correct |
24 |
Correct |
7 ms |
4352 KB |
Output is correct |
25 |
Correct |
6 ms |
4352 KB |
Output is correct |
26 |
Correct |
12 ms |
4352 KB |
Output is correct |
27 |
Correct |
8 ms |
4352 KB |
Output is correct |
28 |
Correct |
7 ms |
4352 KB |
Output is correct |
29 |
Correct |
12 ms |
4352 KB |
Output is correct |
30 |
Correct |
11 ms |
4352 KB |
Output is correct |
31 |
Correct |
237 ms |
71288 KB |
Output is correct |
32 |
Correct |
238 ms |
71288 KB |
Output is correct |
33 |
Correct |
263 ms |
71160 KB |
Output is correct |
34 |
Correct |
559 ms |
71288 KB |
Output is correct |
35 |
Correct |
692 ms |
71288 KB |
Output is correct |
36 |
Correct |
256 ms |
71344 KB |
Output is correct |
37 |
Correct |
186 ms |
59388 KB |
Output is correct |
38 |
Correct |
120 ms |
39928 KB |
Output is correct |
39 |
Correct |
260 ms |
71160 KB |
Output is correct |
40 |
Correct |
264 ms |
71288 KB |
Output is correct |
41 |
Correct |
699 ms |
71160 KB |
Output is correct |
42 |
Correct |
582 ms |
71288 KB |
Output is correct |
43 |
Correct |
334 ms |
71160 KB |
Output is correct |
44 |
Correct |
709 ms |
71288 KB |
Output is correct |
45 |
Correct |
682 ms |
71288 KB |
Output is correct |
46 |
Execution timed out |
5 ms |
768 KB |
Time limit exceeded (wall clock) |
47 |
Halted |
0 ms |
0 KB |
- |