// https://codeforces.com/blog/entry/64479?#comment-484350
#include <iostream>
#include <cstdint>
#include <string>
#include <vector>
using namespace std;
#ifdef DEBUG
#include "debuge.cpp"
#else
#define debug(...) 42
#endif
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vii;
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define SZ(x) ((int) (x).size())
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define ROF(i, a, b) for (int i = (a); i > (b); --i)
void LPP(int &test) {
int n;
cin >> n;
vll a(n + 1), p(n + 1);
vector<pll> dp(n + 1); // number of segment - sum
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) p[i] = p[i - 1] + a[i];
// subtask 4
dp[0].fi = dp[0].se = 0;
for (int i = 1; i <= n; i++) {
dp[i].fi = 0;
dp[i].se = 5e15;
}
dp[0].fi = dp[0].se = 0;
for (int i = 0; i < n; i++) {
debug(i, dp[i]);
if (p[n] - p[i] >= dp[i].se) {
int lo = i, hi = n + 1;
while (lo + 1 < hi) {
int mi = (lo + hi) / 2;
if (p[mi] - p[i] >= dp[i].se) {
hi = mi;
} else {
lo = mi;
}
}
pll tmp;
tmp.fi = dp[i].fi;
tmp.se = dp[i].se + p[hi] - p[i];
if (tmp.fi > dp[hi].fi) {
dp[hi] = tmp;
} else if (tmp.fi == dp[hi].fi) {
dp[hi].se = min(tmp.se, dp[hi].se);
}
tmp.fi = dp[i].fi + 1;
tmp.se = p[hi] - p[i];
if (tmp.fi > dp[hi].fi) {
dp[hi] = tmp;
} else if (tmp.fi == dp[hi].fi) {
dp[hi].se = min(tmp.se, dp[hi].se);
}
debug(hi, dp[hi]);
}
int hi = i + 1;
pll tmp;
tmp.fi = dp[i].fi;
tmp.se = dp[i].se + p[hi] - p[i];
if (tmp.fi > dp[hi].fi) {
dp[hi] = tmp;
} else if (tmp.fi == dp[hi].fi) {
dp[hi].se = min(tmp.se, dp[hi].se);
}
tmp.fi = dp[i].fi + 1;
tmp.se = p[hi] - p[i];
if (tmp.se >= dp[i].se) {
if (tmp.fi > dp[hi].fi) {
dp[hi] = tmp;
} else if (tmp.fi == dp[hi].fi) {
dp[hi].se = min(tmp.se, dp[hi].se);
}
}
debug(i + 1, dp[i + 1]);
}
cout << dp[n].fi;
}
int32_t main() {
cin.tie(0) -> ios::sync_with_stdio(false);
int tc = 1;
// cin >> tc;
for (int i = 1; i <= tc; i++) LPP(i);
}
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
*/
Compilation message
segments.cpp: In function 'void LPP(int&)':
segments.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 42
| ^~
segments.cpp:52:3: note: in expansion of macro 'debug'
52 | debug(i, dp[i]);
| ^~~~~
segments.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 42
| ^~
segments.cpp:78:4: note: in expansion of macro 'debug'
78 | debug(hi, dp[hi]);
| ^~~~~
segments.cpp:12:20: warning: statement has no effect [-Wunused-value]
12 | #define debug(...) 42
| ^~
segments.cpp:98:3: note: in expansion of macro 'debug'
98 | debug(i + 1, dp[i + 1]);
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
580 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
580 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
7 ms |
3968 KB |
Output is correct |
47 |
Correct |
11 ms |
3972 KB |
Output is correct |
48 |
Correct |
11 ms |
4184 KB |
Output is correct |
49 |
Correct |
11 ms |
4444 KB |
Output is correct |
50 |
Correct |
13 ms |
4440 KB |
Output is correct |
51 |
Correct |
17 ms |
4572 KB |
Output is correct |
52 |
Correct |
7 ms |
2396 KB |
Output is correct |
53 |
Correct |
4 ms |
1628 KB |
Output is correct |
54 |
Correct |
10 ms |
3672 KB |
Output is correct |
55 |
Correct |
11 ms |
4584 KB |
Output is correct |
56 |
Correct |
9 ms |
3932 KB |
Output is correct |
57 |
Correct |
9 ms |
3932 KB |
Output is correct |
58 |
Correct |
10 ms |
4056 KB |
Output is correct |
59 |
Correct |
9 ms |
4180 KB |
Output is correct |
60 |
Correct |
9 ms |
3932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
580 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
7 ms |
3968 KB |
Output is correct |
47 |
Correct |
11 ms |
3972 KB |
Output is correct |
48 |
Correct |
11 ms |
4184 KB |
Output is correct |
49 |
Correct |
11 ms |
4444 KB |
Output is correct |
50 |
Correct |
13 ms |
4440 KB |
Output is correct |
51 |
Correct |
17 ms |
4572 KB |
Output is correct |
52 |
Correct |
7 ms |
2396 KB |
Output is correct |
53 |
Correct |
4 ms |
1628 KB |
Output is correct |
54 |
Correct |
10 ms |
3672 KB |
Output is correct |
55 |
Correct |
11 ms |
4584 KB |
Output is correct |
56 |
Correct |
9 ms |
3932 KB |
Output is correct |
57 |
Correct |
9 ms |
3932 KB |
Output is correct |
58 |
Correct |
10 ms |
4056 KB |
Output is correct |
59 |
Correct |
9 ms |
4180 KB |
Output is correct |
60 |
Correct |
9 ms |
3932 KB |
Output is correct |
61 |
Correct |
30 ms |
17924 KB |
Output is correct |
62 |
Correct |
41 ms |
17908 KB |
Output is correct |
63 |
Correct |
54 ms |
18516 KB |
Output is correct |
64 |
Correct |
53 ms |
20068 KB |
Output is correct |
65 |
Correct |
57 ms |
20056 KB |
Output is correct |
66 |
Correct |
62 ms |
20032 KB |
Output is correct |
67 |
Correct |
49 ms |
16220 KB |
Output is correct |
68 |
Correct |
28 ms |
8540 KB |
Output is correct |
69 |
Correct |
48 ms |
16988 KB |
Output is correct |
70 |
Correct |
54 ms |
20052 KB |
Output is correct |
71 |
Correct |
38 ms |
17744 KB |
Output is correct |
72 |
Correct |
45 ms |
18268 KB |
Output is correct |
73 |
Correct |
45 ms |
18516 KB |
Output is correct |
74 |
Correct |
52 ms |
18868 KB |
Output is correct |
75 |
Correct |
45 ms |
18768 KB |
Output is correct |