#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define ppb pop_back
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
using db = double;
using ll = long long;
using ld = long double;
using ii = pair<int, int>;
using iii = tuple<int, int, int>;
using iiii = tuple<int, int, int, int>;
template<class key, class value = null_type, class cmp = less<key> >
using ordered_set = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
int N, ans, d[5005], p[5005], dp[5005][5005];
struct node {
node *left, *right;
int S, E, val, pv;
node(int _s, int _e) : S(_s), E(_e), val(0), pv(0) {
if (S == E) {
return;
}
int M = (S + E) >> 1;
left = new node(S, M);
right = new node(M + 1, E);
}
void prop() {
if (S == E || !pv) return;
left->val += pv;
left->pv += pv;
right->val += pv;
right->pv += pv;
pv = 0;
}
void clear() {
val = pv = 0;
if (S != E) {
left->clear();
right->clear();
}
}
void upd(int l, int r, int v) {
if (l > E || r < S) return;
if (l <= S && E <= r) {
val += v;
pv += v;
return;
}
prop();
left->upd(l, r, v);
right->upd(l, r, v);
val = max(left->val, right->val);
}
} *root;
main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> d[i];
p[i] = p[i - 1] + d[i];
}
root = new node(1, N);
for (int j = N; j >= 1; j--) {
root->clear();
for (int k = j + 1; k <= N; k++) {
root->upd(k, k, dp[j + 1][k]);
}
for (int i = j; i >= 1; i--) {
int lo = j + 1, hi = N, ans = -1;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
if (p[mid] - p[j] == p[j] - p[i - 1]) {
ans = mid;
break;
} else if (p[mid] - p[j] > p[j] - p[i - 1]) hi = mid - 1;
else lo = mid + 1;
}
dp[i][j] = root->val;
//~ cout << "DP " << i << ' ' << j << ' ' << root->val << '\n';
if (ans != -1) {
root->upd(ans + 1, N, 1);
//~ cout << " + " << i << ' ' << ans << '\n';
}
}
}
for (int i = 1; i <= N; i++) {
ans = max(ans, dp[1][i]);
}
cout << ans << '\n';
}
Compilation message
cigle.cpp:71:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
71 | main() {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
3432 KB |
Output is correct |
2 |
Correct |
16 ms |
3432 KB |
Output is correct |
3 |
Correct |
15 ms |
3320 KB |
Output is correct |
4 |
Correct |
18 ms |
3416 KB |
Output is correct |
5 |
Correct |
15 ms |
3412 KB |
Output is correct |
6 |
Correct |
19 ms |
3296 KB |
Output is correct |
7 |
Correct |
16 ms |
3412 KB |
Output is correct |
8 |
Correct |
16 ms |
3412 KB |
Output is correct |
9 |
Correct |
15 ms |
3412 KB |
Output is correct |
10 |
Correct |
15 ms |
3412 KB |
Output is correct |
11 |
Correct |
14 ms |
3412 KB |
Output is correct |
12 |
Correct |
17 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
15 ms |
3432 KB |
Output is correct |
22 |
Correct |
16 ms |
3432 KB |
Output is correct |
23 |
Correct |
15 ms |
3320 KB |
Output is correct |
24 |
Correct |
18 ms |
3416 KB |
Output is correct |
25 |
Correct |
15 ms |
3412 KB |
Output is correct |
26 |
Correct |
19 ms |
3296 KB |
Output is correct |
27 |
Correct |
16 ms |
3412 KB |
Output is correct |
28 |
Correct |
16 ms |
3412 KB |
Output is correct |
29 |
Correct |
15 ms |
3412 KB |
Output is correct |
30 |
Correct |
15 ms |
3412 KB |
Output is correct |
31 |
Correct |
14 ms |
3412 KB |
Output is correct |
32 |
Correct |
17 ms |
3412 KB |
Output is correct |
33 |
Correct |
14 ms |
3388 KB |
Output is correct |
34 |
Correct |
15 ms |
3412 KB |
Output is correct |
35 |
Correct |
15 ms |
3328 KB |
Output is correct |
36 |
Correct |
18 ms |
3432 KB |
Output is correct |
37 |
Correct |
16 ms |
3408 KB |
Output is correct |
38 |
Correct |
16 ms |
3356 KB |
Output is correct |
39 |
Correct |
16 ms |
3412 KB |
Output is correct |
40 |
Correct |
12 ms |
3368 KB |
Output is correct |
41 |
Correct |
12 ms |
3312 KB |
Output is correct |
42 |
Correct |
12 ms |
3412 KB |
Output is correct |
43 |
Correct |
12 ms |
3412 KB |
Output is correct |
44 |
Correct |
12 ms |
3412 KB |
Output is correct |
45 |
Correct |
13 ms |
3424 KB |
Output is correct |
46 |
Correct |
12 ms |
3344 KB |
Output is correct |
47 |
Correct |
12 ms |
3332 KB |
Output is correct |
48 |
Correct |
13 ms |
3412 KB |
Output is correct |
49 |
Correct |
12 ms |
3408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
596 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
15 ms |
3432 KB |
Output is correct |
22 |
Correct |
16 ms |
3432 KB |
Output is correct |
23 |
Correct |
15 ms |
3320 KB |
Output is correct |
24 |
Correct |
18 ms |
3416 KB |
Output is correct |
25 |
Correct |
15 ms |
3412 KB |
Output is correct |
26 |
Correct |
19 ms |
3296 KB |
Output is correct |
27 |
Correct |
16 ms |
3412 KB |
Output is correct |
28 |
Correct |
16 ms |
3412 KB |
Output is correct |
29 |
Correct |
15 ms |
3412 KB |
Output is correct |
30 |
Correct |
15 ms |
3412 KB |
Output is correct |
31 |
Correct |
14 ms |
3412 KB |
Output is correct |
32 |
Correct |
17 ms |
3412 KB |
Output is correct |
33 |
Correct |
14 ms |
3388 KB |
Output is correct |
34 |
Correct |
15 ms |
3412 KB |
Output is correct |
35 |
Correct |
15 ms |
3328 KB |
Output is correct |
36 |
Correct |
18 ms |
3432 KB |
Output is correct |
37 |
Correct |
16 ms |
3408 KB |
Output is correct |
38 |
Correct |
16 ms |
3356 KB |
Output is correct |
39 |
Correct |
16 ms |
3412 KB |
Output is correct |
40 |
Correct |
12 ms |
3368 KB |
Output is correct |
41 |
Correct |
12 ms |
3312 KB |
Output is correct |
42 |
Correct |
12 ms |
3412 KB |
Output is correct |
43 |
Correct |
12 ms |
3412 KB |
Output is correct |
44 |
Correct |
12 ms |
3412 KB |
Output is correct |
45 |
Correct |
13 ms |
3424 KB |
Output is correct |
46 |
Correct |
12 ms |
3344 KB |
Output is correct |
47 |
Correct |
12 ms |
3332 KB |
Output is correct |
48 |
Correct |
13 ms |
3412 KB |
Output is correct |
49 |
Correct |
12 ms |
3408 KB |
Output is correct |
50 |
Execution timed out |
1099 ms |
102344 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |