#ifndef SorahISA
#define SorahISA
#include SorahISA __FILE__ SorahISA
struct BIT {
int maxn;
vector<int> bit;
BIT() {}
BIT(int N) {init(N);}
void init(int N) {
maxn = 1 << (__lg(N) + 1);
bit.assign(maxn, 0);
}
int query(int L, int R) {return _query(R) - _query(L-1);}
int _query(int idx, int ans = 0) {
while (idx) ans += bit[idx], idx -= idx & -idx;
return ans;
}
void add(int idx, int val) {
while (idx < maxn) bit[idx] += val, idx += idx & -idx;
}
};
void solve() {
int N, K; cin >> N >> K;
string S; cin >> S;
vector<int> A(N+1, 0), B(N+1, 0); /// 1-based
for (int i = 0, cA = 0, cB = 0; i < 2*N; ++i) {
if (S[i] == 'A') A[++cA] = i+1;
if (S[i] == 'B') B[++cB] = i+1;
}
// for (int i = 0; i <= N; ++i) debug(i, A[i], B[i]);
BIT bitA, bitB;
vector<vector<int>> inv(N+1, vector<int>(N+1, 0));
for (int a = 1; a <= N; ++a) {
bitA.init(2*N), bitB.init(2*N);
for (int i = N-1; i >= 0; --i) {
bitB.add(B[i+1], 1);
if (i < a) {
inv[i][a] = inv[i+1][a] + bitA.query(B[i+1], 2*N) + bitB.query(1, A[i+1]);
bitA.add(A[i+1], 1);
}
}
}
vector<vector<int>> dp(N+1, vector<int>(N+1, 4*N*N)); dp[0][0] = 0;
vector<vector<int>> fr(N+2, vector<int>(N+1, 0));
for (int k = 1; k <= N; ++k) {
fr[N+1][k] = N;
for (int a = N; a >= 1; --a) {
for (int i = max(int(0), fr[a][k-1]); i <= min(a-1, fr[a+1][k]); ++i) {
if (chmin(dp[a][k], dp[i][k-1] + inv[i][a])) fr[a][k] = i;
}
}
}
cout << dp[N][K] << "\n";
}
int32_t main() {
fastIO();
int t = 1; // cin >> t;
for (int _ = 1; _ <= t; ++_) {
solve();
}
return 0;
}
#else
#ifdef local
#define _GLIBCXX_DEBUG 1
#endif
#pragma GCC optimize("Ofast", "unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define double __float80
using pii = pair<int, int>;
template <typename T> using Prior = std::priority_queue<T>;
template <typename T> using prior = std::priority_queue<T, vector<T>, greater<T>>;
// #define X first
// #define Y second
#define eb emplace_back
#define ef emplace_front
#define ee emplace
#define pb pop_back
#define pf pop_front
#define ALL(x) begin(x), end(x)
#define RALL(x) rbegin(x), rend(x)
#define SZ(x) ((int)(x).size())
#ifdef local
#define fastIO() void()
#define debug(...) \
fprintf(stderr, "\u001b[33mAt [%s], line %d: (%s) = ", __FUNCTION__, __LINE__, #__VA_ARGS__), \
_do(__VA_ARGS__), fprintf(stderr, "\u001b[0m")
template <typename T> void _do(T &&_t) {cerr << _t << "\n";}
template <typename T, typename ...U> void _do(T &&_t, U &&..._u) {cerr << _t << ", ", _do(_u...);}
#else
#define fastIO() ios_base::sync_with_stdio(0), cin.tie(0)
#define debug(...) void()
#endif
template <typename T, typename U> bool chmin(T &lhs, U rhs) {return lhs > rhs ? lhs = rhs, 1 : 0;}
template <typename T, typename U> bool chmax(T &lhs, U rhs) {return lhs < rhs ? lhs = rhs, 1 : 0;}
#endif
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
1 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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
1 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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
1112 KB |
Output is correct |
18 |
Correct |
7 ms |
6236 KB |
Output is correct |
19 |
Correct |
8 ms |
6236 KB |
Output is correct |
20 |
Correct |
9 ms |
6236 KB |
Output is correct |
21 |
Correct |
7 ms |
6236 KB |
Output is correct |
22 |
Correct |
9 ms |
6236 KB |
Output is correct |
23 |
Correct |
7 ms |
6236 KB |
Output is correct |
24 |
Correct |
7 ms |
6428 KB |
Output is correct |
25 |
Correct |
7 ms |
6236 KB |
Output is correct |
26 |
Correct |
7 ms |
6232 KB |
Output is correct |
27 |
Correct |
7 ms |
6300 KB |
Output is correct |
28 |
Correct |
7 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
1 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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
1112 KB |
Output is correct |
18 |
Correct |
7 ms |
6236 KB |
Output is correct |
19 |
Correct |
8 ms |
6236 KB |
Output is correct |
20 |
Correct |
9 ms |
6236 KB |
Output is correct |
21 |
Correct |
7 ms |
6236 KB |
Output is correct |
22 |
Correct |
9 ms |
6236 KB |
Output is correct |
23 |
Correct |
7 ms |
6236 KB |
Output is correct |
24 |
Correct |
7 ms |
6428 KB |
Output is correct |
25 |
Correct |
7 ms |
6236 KB |
Output is correct |
26 |
Correct |
7 ms |
6232 KB |
Output is correct |
27 |
Correct |
7 ms |
6300 KB |
Output is correct |
28 |
Correct |
7 ms |
6236 KB |
Output is correct |
29 |
Correct |
1888 ms |
569564 KB |
Output is correct |
30 |
Correct |
2073 ms |
588712 KB |
Output is correct |
31 |
Correct |
2051 ms |
588680 KB |
Output is correct |
32 |
Correct |
1750 ms |
588676 KB |
Output is correct |
33 |
Correct |
1748 ms |
588716 KB |
Output is correct |
34 |
Correct |
1741 ms |
588688 KB |
Output is correct |
35 |
Correct |
1785 ms |
588624 KB |
Output is correct |
36 |
Correct |
2005 ms |
588464 KB |
Output is correct |
37 |
Correct |
1985 ms |
588684 KB |
Output is correct |
38 |
Correct |
1834 ms |
588468 KB |
Output is correct |
39 |
Correct |
1912 ms |
588680 KB |
Output is correct |
40 |
Correct |
1693 ms |
588560 KB |
Output is correct |
41 |
Correct |
1699 ms |
588616 KB |
Output is correct |
42 |
Correct |
1786 ms |
588652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
1 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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
1112 KB |
Output is correct |
18 |
Correct |
7 ms |
6236 KB |
Output is correct |
19 |
Correct |
8 ms |
6236 KB |
Output is correct |
20 |
Correct |
9 ms |
6236 KB |
Output is correct |
21 |
Correct |
7 ms |
6236 KB |
Output is correct |
22 |
Correct |
9 ms |
6236 KB |
Output is correct |
23 |
Correct |
7 ms |
6236 KB |
Output is correct |
24 |
Correct |
7 ms |
6428 KB |
Output is correct |
25 |
Correct |
7 ms |
6236 KB |
Output is correct |
26 |
Correct |
7 ms |
6232 KB |
Output is correct |
27 |
Correct |
7 ms |
6300 KB |
Output is correct |
28 |
Correct |
7 ms |
6236 KB |
Output is correct |
29 |
Correct |
1888 ms |
569564 KB |
Output is correct |
30 |
Correct |
2073 ms |
588712 KB |
Output is correct |
31 |
Correct |
2051 ms |
588680 KB |
Output is correct |
32 |
Correct |
1750 ms |
588676 KB |
Output is correct |
33 |
Correct |
1748 ms |
588716 KB |
Output is correct |
34 |
Correct |
1741 ms |
588688 KB |
Output is correct |
35 |
Correct |
1785 ms |
588624 KB |
Output is correct |
36 |
Correct |
2005 ms |
588464 KB |
Output is correct |
37 |
Correct |
1985 ms |
588684 KB |
Output is correct |
38 |
Correct |
1834 ms |
588468 KB |
Output is correct |
39 |
Correct |
1912 ms |
588680 KB |
Output is correct |
40 |
Correct |
1693 ms |
588560 KB |
Output is correct |
41 |
Correct |
1699 ms |
588616 KB |
Output is correct |
42 |
Correct |
1786 ms |
588652 KB |
Output is correct |
43 |
Runtime error |
460 ms |
1048576 KB |
Execution killed with signal 9 |
44 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
1 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 |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
1112 KB |
Output is correct |
18 |
Correct |
7 ms |
6236 KB |
Output is correct |
19 |
Correct |
8 ms |
6236 KB |
Output is correct |
20 |
Correct |
9 ms |
6236 KB |
Output is correct |
21 |
Correct |
7 ms |
6236 KB |
Output is correct |
22 |
Correct |
9 ms |
6236 KB |
Output is correct |
23 |
Correct |
7 ms |
6236 KB |
Output is correct |
24 |
Correct |
7 ms |
6428 KB |
Output is correct |
25 |
Correct |
7 ms |
6236 KB |
Output is correct |
26 |
Correct |
7 ms |
6232 KB |
Output is correct |
27 |
Correct |
7 ms |
6300 KB |
Output is correct |
28 |
Correct |
7 ms |
6236 KB |
Output is correct |
29 |
Correct |
1888 ms |
569564 KB |
Output is correct |
30 |
Correct |
2073 ms |
588712 KB |
Output is correct |
31 |
Correct |
2051 ms |
588680 KB |
Output is correct |
32 |
Correct |
1750 ms |
588676 KB |
Output is correct |
33 |
Correct |
1748 ms |
588716 KB |
Output is correct |
34 |
Correct |
1741 ms |
588688 KB |
Output is correct |
35 |
Correct |
1785 ms |
588624 KB |
Output is correct |
36 |
Correct |
2005 ms |
588464 KB |
Output is correct |
37 |
Correct |
1985 ms |
588684 KB |
Output is correct |
38 |
Correct |
1834 ms |
588468 KB |
Output is correct |
39 |
Correct |
1912 ms |
588680 KB |
Output is correct |
40 |
Correct |
1693 ms |
588560 KB |
Output is correct |
41 |
Correct |
1699 ms |
588616 KB |
Output is correct |
42 |
Correct |
1786 ms |
588652 KB |
Output is correct |
43 |
Runtime error |
460 ms |
1048576 KB |
Execution killed with signal 9 |
44 |
Halted |
0 ms |
0 KB |
- |