#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <array>
#define ll long long
using namespace std;
ll n, k, dp[2][1000000];
vector <ll> A, B, nxA, ps;
string S;
ll C(ll l, ll r) {
if (nxA[l] > r) return 0;
ll nx = max(l, nxA[l]), cnt = r-nx+1, cnt2 = max(0LL, l-nxA[l]);
return ps[r]-(nx ? ps[nx-1] : 0)-cnt*B[l]-cnt*(cnt-1)/2-(r-l+1)*cnt2;
}
void solve(ll l, ll r, ll ql, ll qr) {
ll mid = (l+r)/2;
ll opt = 1e18, optid = -1, tmp;
for (int i=ql; i<mid; ++i) {
tmp = dp[0][i] + C(i+1, mid);
if (opt > tmp) {
opt = tmp;
optid = i;
}
}
dp[1][mid] = opt;
if (l < mid) solve(l, mid-1, ql, optid);
if (mid < r) solve(mid+1, r, optid, qr);
}
int main() {
cin >> n >> k >> S;
for (int i=0; i<2*n; ++i) {
if (S[i] == 'A') {
ps.push_back((ps.empty() ? 0 : ps.back()) + i);
A.push_back(i);
}
else {
B.push_back(i);
nxA.push_back(A.size());
}
}
for (int i=0; i<n; ++i) dp[0][i] = C(0, i);
for (int x=1; x<k; ++x) {
solve(0, n-1, 0, n-1);
swap(dp[0], dp[1]);
}
cout << dp[0][n-1] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
7 ms |
15964 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
16056 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
15964 KB |
Output is correct |
7 |
Correct |
7 ms |
15964 KB |
Output is correct |
8 |
Correct |
6 ms |
15964 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
15892 KB |
Output is correct |
11 |
Correct |
10 ms |
15964 KB |
Output is correct |
12 |
Correct |
11 ms |
15988 KB |
Output is correct |
13 |
Correct |
13 ms |
15960 KB |
Output is correct |
14 |
Correct |
6 ms |
15876 KB |
Output is correct |
15 |
Correct |
7 ms |
15964 KB |
Output is correct |
16 |
Correct |
7 ms |
15964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
7 ms |
15964 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
16056 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
15964 KB |
Output is correct |
7 |
Correct |
7 ms |
15964 KB |
Output is correct |
8 |
Correct |
6 ms |
15964 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
15892 KB |
Output is correct |
11 |
Correct |
10 ms |
15964 KB |
Output is correct |
12 |
Correct |
11 ms |
15988 KB |
Output is correct |
13 |
Correct |
13 ms |
15960 KB |
Output is correct |
14 |
Correct |
6 ms |
15876 KB |
Output is correct |
15 |
Correct |
7 ms |
15964 KB |
Output is correct |
16 |
Correct |
7 ms |
15964 KB |
Output is correct |
17 |
Correct |
38 ms |
15964 KB |
Output is correct |
18 |
Correct |
62 ms |
16096 KB |
Output is correct |
19 |
Correct |
177 ms |
16096 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
6 ms |
15872 KB |
Output is correct |
22 |
Correct |
318 ms |
15964 KB |
Output is correct |
23 |
Correct |
337 ms |
16096 KB |
Output is correct |
24 |
Correct |
12 ms |
15960 KB |
Output is correct |
25 |
Correct |
311 ms |
15960 KB |
Output is correct |
26 |
Correct |
266 ms |
15960 KB |
Output is correct |
27 |
Correct |
109 ms |
15960 KB |
Output is correct |
28 |
Correct |
109 ms |
16096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
7 ms |
15964 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
16056 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
15964 KB |
Output is correct |
7 |
Correct |
7 ms |
15964 KB |
Output is correct |
8 |
Correct |
6 ms |
15964 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
15892 KB |
Output is correct |
11 |
Correct |
10 ms |
15964 KB |
Output is correct |
12 |
Correct |
11 ms |
15988 KB |
Output is correct |
13 |
Correct |
13 ms |
15960 KB |
Output is correct |
14 |
Correct |
6 ms |
15876 KB |
Output is correct |
15 |
Correct |
7 ms |
15964 KB |
Output is correct |
16 |
Correct |
7 ms |
15964 KB |
Output is correct |
17 |
Correct |
38 ms |
15964 KB |
Output is correct |
18 |
Correct |
62 ms |
16096 KB |
Output is correct |
19 |
Correct |
177 ms |
16096 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
6 ms |
15872 KB |
Output is correct |
22 |
Correct |
318 ms |
15964 KB |
Output is correct |
23 |
Correct |
337 ms |
16096 KB |
Output is correct |
24 |
Correct |
12 ms |
15960 KB |
Output is correct |
25 |
Correct |
311 ms |
15960 KB |
Output is correct |
26 |
Correct |
266 ms |
15960 KB |
Output is correct |
27 |
Correct |
109 ms |
15960 KB |
Output is correct |
28 |
Correct |
109 ms |
16096 KB |
Output is correct |
29 |
Correct |
1712 ms |
16368 KB |
Output is correct |
30 |
Correct |
5313 ms |
16220 KB |
Output is correct |
31 |
Correct |
1452 ms |
16412 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
21 ms |
16172 KB |
Output is correct |
34 |
Correct |
3840 ms |
16392 KB |
Output is correct |
35 |
Correct |
3819 ms |
16212 KB |
Output is correct |
36 |
Correct |
73 ms |
16196 KB |
Output is correct |
37 |
Correct |
986 ms |
16408 KB |
Output is correct |
38 |
Correct |
3179 ms |
16212 KB |
Output is correct |
39 |
Correct |
2070 ms |
16404 KB |
Output is correct |
40 |
Correct |
1493 ms |
16180 KB |
Output is correct |
41 |
Correct |
1478 ms |
16216 KB |
Output is correct |
42 |
Execution timed out |
7037 ms |
16216 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
7 ms |
15964 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
16056 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
15964 KB |
Output is correct |
7 |
Correct |
7 ms |
15964 KB |
Output is correct |
8 |
Correct |
6 ms |
15964 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
15892 KB |
Output is correct |
11 |
Correct |
10 ms |
15964 KB |
Output is correct |
12 |
Correct |
11 ms |
15988 KB |
Output is correct |
13 |
Correct |
13 ms |
15960 KB |
Output is correct |
14 |
Correct |
6 ms |
15876 KB |
Output is correct |
15 |
Correct |
7 ms |
15964 KB |
Output is correct |
16 |
Correct |
7 ms |
15964 KB |
Output is correct |
17 |
Correct |
38 ms |
15964 KB |
Output is correct |
18 |
Correct |
62 ms |
16096 KB |
Output is correct |
19 |
Correct |
177 ms |
16096 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
6 ms |
15872 KB |
Output is correct |
22 |
Correct |
318 ms |
15964 KB |
Output is correct |
23 |
Correct |
337 ms |
16096 KB |
Output is correct |
24 |
Correct |
12 ms |
15960 KB |
Output is correct |
25 |
Correct |
311 ms |
15960 KB |
Output is correct |
26 |
Correct |
266 ms |
15960 KB |
Output is correct |
27 |
Correct |
109 ms |
15960 KB |
Output is correct |
28 |
Correct |
109 ms |
16096 KB |
Output is correct |
29 |
Correct |
1712 ms |
16368 KB |
Output is correct |
30 |
Correct |
5313 ms |
16220 KB |
Output is correct |
31 |
Correct |
1452 ms |
16412 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
21 ms |
16172 KB |
Output is correct |
34 |
Correct |
3840 ms |
16392 KB |
Output is correct |
35 |
Correct |
3819 ms |
16212 KB |
Output is correct |
36 |
Correct |
73 ms |
16196 KB |
Output is correct |
37 |
Correct |
986 ms |
16408 KB |
Output is correct |
38 |
Correct |
3179 ms |
16212 KB |
Output is correct |
39 |
Correct |
2070 ms |
16404 KB |
Output is correct |
40 |
Correct |
1493 ms |
16180 KB |
Output is correct |
41 |
Correct |
1478 ms |
16216 KB |
Output is correct |
42 |
Execution timed out |
7037 ms |
16216 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
15964 KB |
Output is correct |
2 |
Correct |
7 ms |
15964 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
6 ms |
16056 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
7 ms |
15964 KB |
Output is correct |
7 |
Correct |
7 ms |
15964 KB |
Output is correct |
8 |
Correct |
6 ms |
15964 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
6 ms |
15892 KB |
Output is correct |
11 |
Correct |
10 ms |
15964 KB |
Output is correct |
12 |
Correct |
11 ms |
15988 KB |
Output is correct |
13 |
Correct |
13 ms |
15960 KB |
Output is correct |
14 |
Correct |
6 ms |
15876 KB |
Output is correct |
15 |
Correct |
7 ms |
15964 KB |
Output is correct |
16 |
Correct |
7 ms |
15964 KB |
Output is correct |
17 |
Correct |
38 ms |
15964 KB |
Output is correct |
18 |
Correct |
62 ms |
16096 KB |
Output is correct |
19 |
Correct |
177 ms |
16096 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
6 ms |
15872 KB |
Output is correct |
22 |
Correct |
318 ms |
15964 KB |
Output is correct |
23 |
Correct |
337 ms |
16096 KB |
Output is correct |
24 |
Correct |
12 ms |
15960 KB |
Output is correct |
25 |
Correct |
311 ms |
15960 KB |
Output is correct |
26 |
Correct |
266 ms |
15960 KB |
Output is correct |
27 |
Correct |
109 ms |
15960 KB |
Output is correct |
28 |
Correct |
109 ms |
16096 KB |
Output is correct |
29 |
Correct |
1712 ms |
16368 KB |
Output is correct |
30 |
Correct |
5313 ms |
16220 KB |
Output is correct |
31 |
Correct |
1452 ms |
16412 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
21 ms |
16172 KB |
Output is correct |
34 |
Correct |
3840 ms |
16392 KB |
Output is correct |
35 |
Correct |
3819 ms |
16212 KB |
Output is correct |
36 |
Correct |
73 ms |
16196 KB |
Output is correct |
37 |
Correct |
986 ms |
16408 KB |
Output is correct |
38 |
Correct |
3179 ms |
16212 KB |
Output is correct |
39 |
Correct |
2070 ms |
16404 KB |
Output is correct |
40 |
Correct |
1493 ms |
16180 KB |
Output is correct |
41 |
Correct |
1478 ms |
16216 KB |
Output is correct |
42 |
Execution timed out |
7037 ms |
16216 KB |
Time limit exceeded |
43 |
Halted |
0 ms |
0 KB |
- |