// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define pb push_back
#define sz(x) int(x.size())
using str = string;
template<class T> using V = vector<T>;
const int INF = int(1e9) + 10;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N; cin >> N;
str S; cin >> S;
V<V<int>> oc(3); for(int i = 0; i < N; i++) {
int t = (S[i] == 'R' ? 0 : (S[i] == 'G' ? 1 : 2));
oc[t].pb(i);
}
sort(begin(oc), end(oc), [&](const V<int> &a, const V<int> &b) {
return size(a) < size(b);
});
int A = sz(oc[0]), B = sz(oc[1]), C = N - A - B;
V<V<V<int>>> COST(9);
for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) if (i != j) {
COST[3 * i + j] = V<V<int>>(sz(oc[i]), V<int>(sz(oc[j]) + 1));
for(int x = 0; x < sz(oc[i]); x++) for(int y = 0; y <= sz(oc[j]); y++) {
COST[3 * i + j][x][y] = (lower_bound(begin(oc[j]) + y, end(oc[j]), oc[i][x]) - (begin(oc[j]) + y));
// cout << 3 * i + j << " " << x << " " << y << endl;
// cout << COST[3 * i + j][x][y] << endl;
}
}
V<V<V<int>>> dp(A + 1, V<V<int>>(B + 1, V<int>(3, INF)));
dp[0][0] = {0, 0, 0};
for(int i = 0; i < N; i++) {
V<V<V<int>>> ndp(A + 1, V<V<int>>(B + 1, V<int>(3, INF)));
for(int a = 0; a <= A; a++) for(int b = 0; b <= B; b++) for(int lst = 0; lst < 3; lst++) {
if (dp[a][b][lst] == INF) continue;
int c = i - a - b; if (c < 0) continue;
// cout << i << " " << a << " " << b << " " << c << " " << lst << endl;
// cout << dp[a][b][lst] << endl;
// cout << endl;
if (a + 1 <= A && lst != 0)
ndp[a+1][b][0] = min(ndp[a+1][b][0], dp[a][b][lst] + COST[1][a][b] + COST[2][a][c]);
// cout << 1 << endl;
if (b + 1 <= B && lst != 1)
ndp[a][b+1][1] = min(ndp[a][b+1][1], dp[a][b][lst] + COST[3][b][a] + COST[5][b][c]);
// cout << 1 << endl;
if (c + 1 <= C && lst != 2)
ndp[a][b][2] = min(ndp[a][b][2], dp[a][b][lst] + COST[6][c][a] + COST[7][c][b]);
}
// cout << endl;
dp.swap(ndp);
}
int ans = min({dp[A][B][0], dp[A][B][1], dp[A][B][2]});
cout << (ans == INF ? -1 : ans) << nl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
380 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
320 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
320 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
316 KB |
Output is correct |
37 |
Correct |
1 ms |
316 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
596 KB |
Output is correct |
3 |
Correct |
7 ms |
704 KB |
Output is correct |
4 |
Correct |
7 ms |
596 KB |
Output is correct |
5 |
Correct |
7 ms |
596 KB |
Output is correct |
6 |
Correct |
8 ms |
712 KB |
Output is correct |
7 |
Correct |
7 ms |
700 KB |
Output is correct |
8 |
Correct |
7 ms |
596 KB |
Output is correct |
9 |
Correct |
7 ms |
596 KB |
Output is correct |
10 |
Correct |
7 ms |
704 KB |
Output is correct |
11 |
Correct |
7 ms |
700 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
4 ms |
468 KB |
Output is correct |
14 |
Correct |
5 ms |
596 KB |
Output is correct |
15 |
Correct |
7 ms |
596 KB |
Output is correct |
16 |
Correct |
7 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
316 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
316 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
1 ms |
380 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
320 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
320 KB |
Output is correct |
34 |
Correct |
1 ms |
324 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
316 KB |
Output is correct |
37 |
Correct |
1 ms |
316 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
320 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
6 ms |
596 KB |
Output is correct |
44 |
Correct |
7 ms |
704 KB |
Output is correct |
45 |
Correct |
7 ms |
596 KB |
Output is correct |
46 |
Correct |
7 ms |
596 KB |
Output is correct |
47 |
Correct |
8 ms |
712 KB |
Output is correct |
48 |
Correct |
7 ms |
700 KB |
Output is correct |
49 |
Correct |
7 ms |
596 KB |
Output is correct |
50 |
Correct |
7 ms |
596 KB |
Output is correct |
51 |
Correct |
7 ms |
704 KB |
Output is correct |
52 |
Correct |
7 ms |
700 KB |
Output is correct |
53 |
Correct |
2 ms |
340 KB |
Output is correct |
54 |
Correct |
4 ms |
468 KB |
Output is correct |
55 |
Correct |
5 ms |
596 KB |
Output is correct |
56 |
Correct |
7 ms |
596 KB |
Output is correct |
57 |
Correct |
7 ms |
596 KB |
Output is correct |
58 |
Correct |
245 ms |
2496 KB |
Output is correct |
59 |
Correct |
249 ms |
2516 KB |
Output is correct |
60 |
Correct |
247 ms |
2596 KB |
Output is correct |
61 |
Correct |
263 ms |
2712 KB |
Output is correct |
62 |
Correct |
16 ms |
724 KB |
Output is correct |
63 |
Correct |
28 ms |
884 KB |
Output is correct |
64 |
Correct |
94 ms |
1400 KB |
Output is correct |
65 |
Correct |
138 ms |
1748 KB |
Output is correct |
66 |
Correct |
259 ms |
2664 KB |
Output is correct |
67 |
Correct |
228 ms |
2364 KB |
Output is correct |
68 |
Correct |
268 ms |
2692 KB |
Output is correct |
69 |
Correct |
263 ms |
2644 KB |
Output is correct |
70 |
Correct |
254 ms |
2636 KB |
Output is correct |
71 |
Correct |
231 ms |
2472 KB |
Output is correct |
72 |
Correct |
31 ms |
724 KB |
Output is correct |
73 |
Correct |
11 ms |
596 KB |
Output is correct |