#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int> a, b;
for (int i = 0; i < 2 * n; i++) {
if (s[i] == 'A') {
a.push_back(i);
} else {
b.push_back(i);
}
}
vector<int> ia(2 * n);
for (int i = 0; i < n; i++) {
ia[a[i]] = i + 1;
}
vector<int> pref(2 * n);
for (int i = 0; i < 2 * n; i++) {
if (i > 0) {
pref[i] = pref[i - 1];
}
pref[i] += (s[i] == 'A' ? 1 : 0);
}
vector<int> ib(2 * n);
for (int i = 0; i < n; i++) {
ib[b[i]] = pref[b[i]];
}
auto Solve = [&](int L, int R) {
int res = 0;
for (int i = L; i <= R; i++) {
res += max(0, ia[a[R]] - ib[b[i]]);
}
return res;
};
for (int i = 0; i < n; i++) {
if (i > 0) {
pref[i] = pref[i - 1];
} else {
pref[i] = 0;
}
pref[i] += ib[b[i]];
}
auto GetSum = [&](int L, int R) {
return pref[R] - (L == 0 ? 0 : pref[L - 1]);
};
const int inf = (int) 1e9;
vector<int> dp(n + 1, inf);
dp[0] = 0;
for (int foo = 0; foo < k; foo++) {
vector<int> new_dp(n + 1, inf);
int ptr = -1;
for (int i = 0; i < n; i++) {
while (ptr + 1 <= i && ia[a[i]] >= ib[b[ptr + 1]]) {
ptr += 1;
}
for (int j = ptr + 1; j <= i; j++) {
new_dp[i + 1] = min(new_dp[i + 1], dp[j]);
}
for (int j = 0; j <= ptr; j++) {
new_dp[i + 1] = min(new_dp[i + 1], dp[j] + ia[a[i]] * (ptr - j + 1) - GetSum(j, ptr));
}
}
swap(dp, new_dp);
}
cout << dp[n] << '\n';
return 0;
}
Compilation message
chorus.cpp: In function 'int main()':
chorus.cpp:35:8: warning: variable 'Solve' set but not used [-Wunused-but-set-variable]
35 | auto Solve = [&](int L, int R) {
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 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 |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
17 ms |
472 KB |
Output is correct |
19 |
Correct |
50 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
94 ms |
456 KB |
Output is correct |
23 |
Correct |
106 ms |
452 KB |
Output is correct |
24 |
Correct |
3 ms |
480 KB |
Output is correct |
25 |
Correct |
86 ms |
460 KB |
Output is correct |
26 |
Correct |
87 ms |
348 KB |
Output is correct |
27 |
Correct |
29 ms |
344 KB |
Output is correct |
28 |
Correct |
29 ms |
456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 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 |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
17 ms |
472 KB |
Output is correct |
19 |
Correct |
50 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
94 ms |
456 KB |
Output is correct |
23 |
Correct |
106 ms |
452 KB |
Output is correct |
24 |
Correct |
3 ms |
480 KB |
Output is correct |
25 |
Correct |
86 ms |
460 KB |
Output is correct |
26 |
Correct |
87 ms |
348 KB |
Output is correct |
27 |
Correct |
29 ms |
344 KB |
Output is correct |
28 |
Correct |
29 ms |
456 KB |
Output is correct |
29 |
Execution timed out |
7080 ms |
600 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 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 |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
17 ms |
472 KB |
Output is correct |
19 |
Correct |
50 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
94 ms |
456 KB |
Output is correct |
23 |
Correct |
106 ms |
452 KB |
Output is correct |
24 |
Correct |
3 ms |
480 KB |
Output is correct |
25 |
Correct |
86 ms |
460 KB |
Output is correct |
26 |
Correct |
87 ms |
348 KB |
Output is correct |
27 |
Correct |
29 ms |
344 KB |
Output is correct |
28 |
Correct |
29 ms |
456 KB |
Output is correct |
29 |
Execution timed out |
7080 ms |
600 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 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 |
3 ms |
348 KB |
Output is correct |
18 |
Correct |
17 ms |
472 KB |
Output is correct |
19 |
Correct |
50 ms |
456 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
94 ms |
456 KB |
Output is correct |
23 |
Correct |
106 ms |
452 KB |
Output is correct |
24 |
Correct |
3 ms |
480 KB |
Output is correct |
25 |
Correct |
86 ms |
460 KB |
Output is correct |
26 |
Correct |
87 ms |
348 KB |
Output is correct |
27 |
Correct |
29 ms |
344 KB |
Output is correct |
28 |
Correct |
29 ms |
456 KB |
Output is correct |
29 |
Execution timed out |
7080 ms |
600 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |