#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 5e3+10, MOD = 1e9+7;
const ll INF = 1e18+10;
int n, K, small[N];
ll ps[N], l_use[N];
vector<int> one, two;
// min CHT with
// - slopes decreasing
// - query x increasing
struct Line {
ll m, b;
ll calc(ll x) {
return m * x + b;
}
};
struct CHT {
deque<Line> lines;
void add(ll m, ll b) {
while (sz(lines) >= 2) {
auto A = lines.end()[-2];
auto B = lines.end()[-1];
if ((b - A.b) * (A.m - B.m) <= (B.b - A.b) * (A.m - m)) lines.pop_back();
else break;
}
lines.push_back({m, b});
}
ll qry(ll x) {
while (sz(lines) >= 2 && lines[0].calc(x) >= lines[1].calc(x)) {
lines.pop_front();
}
return lines[0].calc(x);
}
};
void solve() {
cin >> n >> K;
for (int i = 0; i < 2 * n; i++) {
char c; cin >> c;
if (c == 'A') one.push_back(i);
else two.push_back(i);
}
for (int i = 0; i < n; i++) {
small[i] = lower_bound(two.begin(), two.end(), one[i]) - two.begin();
}
for (int i = 0; i < n; i++) {
ps[i] = small[i] + (i ? ps[i-1] : 0);
}
auto calc_sum = [&](int L, int R) {
return (long long) (L + R) * (R - L + 1) / 2;
};
for (int i = 0; i < n; i++) {
l_use[i] = -(i ? ps[i-1] : 0);
for (int j = i; j < n; j++) {
if (small[j] < i) {
l_use[i] -= small[j] - i;
}
}
/*
int use = lower_bound(small, small + n, i) - small - 1; // [i..use]
if (use >= i) {
l_use[i] -= ps[use] - (i ? ps[i-1] : 0) - calc_sum(i, use);
}
*/
}
vector<ll> prv(n, INF);
for (int k = 1; k <= K; k++) {
vector<ll> nxt(n, INF);
CHT lines;
for (int i = 0; i < n; i++) {
lines.add(-i, (long long) i * i - i + l_use[i] + (i == 0 ? 0 : prv[i-1]));
nxt[i] = lines.qry(i) + ps[i];
int l = small[i];
if (l <= i) {
nxt[i] = min(nxt[i], (l == 0 ? 0 : prv[l-1]));
}
}
swap(prv, nxt);
}
cout << prv[n-1] << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
Compilation message
chorus.cpp: In function 'void solve()':
chorus.cpp:65:10: warning: variable 'calc_sum' set but not used [-Wunused-but-set-variable]
65 | auto calc_sum = [&](int L, int R) {
| ^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
264 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
264 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
3 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
264 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
3 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
324 KB |
Output is correct |
29 |
Correct |
158 ms |
592 KB |
Output is correct |
30 |
Correct |
429 ms |
596 KB |
Output is correct |
31 |
Correct |
111 ms |
592 KB |
Output is correct |
32 |
Correct |
9 ms |
596 KB |
Output is correct |
33 |
Correct |
9 ms |
596 KB |
Output is correct |
34 |
Correct |
252 ms |
620 KB |
Output is correct |
35 |
Correct |
261 ms |
596 KB |
Output is correct |
36 |
Correct |
10 ms |
512 KB |
Output is correct |
37 |
Correct |
100 ms |
604 KB |
Output is correct |
38 |
Correct |
235 ms |
636 KB |
Output is correct |
39 |
Correct |
172 ms |
620 KB |
Output is correct |
40 |
Correct |
100 ms |
584 KB |
Output is correct |
41 |
Correct |
103 ms |
588 KB |
Output is correct |
42 |
Correct |
94 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
264 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
3 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
324 KB |
Output is correct |
29 |
Correct |
158 ms |
592 KB |
Output is correct |
30 |
Correct |
429 ms |
596 KB |
Output is correct |
31 |
Correct |
111 ms |
592 KB |
Output is correct |
32 |
Correct |
9 ms |
596 KB |
Output is correct |
33 |
Correct |
9 ms |
596 KB |
Output is correct |
34 |
Correct |
252 ms |
620 KB |
Output is correct |
35 |
Correct |
261 ms |
596 KB |
Output is correct |
36 |
Correct |
10 ms |
512 KB |
Output is correct |
37 |
Correct |
100 ms |
604 KB |
Output is correct |
38 |
Correct |
235 ms |
636 KB |
Output is correct |
39 |
Correct |
172 ms |
620 KB |
Output is correct |
40 |
Correct |
100 ms |
584 KB |
Output is correct |
41 |
Correct |
103 ms |
588 KB |
Output is correct |
42 |
Correct |
94 ms |
596 KB |
Output is correct |
43 |
Runtime error |
4 ms |
2032 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
264 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
3 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
324 KB |
Output is correct |
29 |
Correct |
158 ms |
592 KB |
Output is correct |
30 |
Correct |
429 ms |
596 KB |
Output is correct |
31 |
Correct |
111 ms |
592 KB |
Output is correct |
32 |
Correct |
9 ms |
596 KB |
Output is correct |
33 |
Correct |
9 ms |
596 KB |
Output is correct |
34 |
Correct |
252 ms |
620 KB |
Output is correct |
35 |
Correct |
261 ms |
596 KB |
Output is correct |
36 |
Correct |
10 ms |
512 KB |
Output is correct |
37 |
Correct |
100 ms |
604 KB |
Output is correct |
38 |
Correct |
235 ms |
636 KB |
Output is correct |
39 |
Correct |
172 ms |
620 KB |
Output is correct |
40 |
Correct |
100 ms |
584 KB |
Output is correct |
41 |
Correct |
103 ms |
588 KB |
Output is correct |
42 |
Correct |
94 ms |
596 KB |
Output is correct |
43 |
Runtime error |
4 ms |
2032 KB |
Execution killed with signal 11 |
44 |
Halted |
0 ms |
0 KB |
- |