#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 4e2 + 7;
const int INF = (int) 1e9;
int n;
int a[N];
int cnt[3][N][3];
int dp[N][N][N][3];
string s;
int decode(char ch) {
if (ch == 'R') {
return 0;
} else if (ch == 'G') {
return 1;
} else if (ch == 'Y') {
return 2;
}
assert(0);
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
// freopen("input", "r", stdin);
cin >> n >> s;
for (int i = 1; i <= n; i++) {
a[i] = decode(s[i - 1]);
}
int cnta = 0, cntb = 0, cntc = 0;
for (int i = 1; i <= n; i++) {
if (a[i] == 0) {
cnta++;
cnt[0][cnta][1] = cntb;
cnt[0][cnta][2] = cntc;
} else if (a[i] == 1) {
cntb++;
cnt[1][cntb][0] = cnta;
cnt[1][cntb][2] = cntc;
} else {
cntc++;
cnt[2][cntc][0] = cnta;
cnt[2][cntc][1] = cntb;
}
}
for (int a = 0; a <= cnta; a++) {
for (int b = 0; b <= cntb; b++) {
for (int c = 0; c <= cntc; c++) {
dp[a][b][c][0] = INF;
dp[a][b][c][1] = INF;
dp[a][b][c][2] = INF;
}
}
}
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (int a = 0; a <= cnta; a++) {
for (int b = 0; b <= cntb; b++) {
for (int c = 0; c <= cntc; c++) {
if (a < cnta) {
dp[a + 1][b][c][0] = min(dp[a + 1][b][c][0], min(dp[a][b][c][1], dp[a][b][c][2]) + abs(b - cnt[0][a + 1][1]) + abs(c - cnt[0][a + 1][2]));
}
if (b < cntb) {
dp[a][b + 1][c][1] = min(dp[a][b + 1][c][1], min(dp[a][b][c][0], dp[a][b][c][2]) + abs(a - cnt[1][b + 1][0]) + abs(c - cnt[1][b + 1][2]));
}
if (c < cntc) {
dp[a][b][c + 1][2] = min(dp[a][b][c + 1][2], min(dp[a][b][c][0], dp[a][b][c][1]) + abs(a - cnt[2][c + 1][0]) + abs(b - cnt[2][c + 1][1]));
}
}
}
}
int sol = INF;
sol = min(sol, dp[cnta][cntb][cntc][0]);
sol = min(sol, dp[cnta][cntb][cntc][1]);
sol = min(sol, dp[cnta][cntb][cntc][2]);
if (sol == INF) {
cout << "-1\n";
return 0;
}
cout << sol / 2;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
2244 KB |
Output is correct |
19 |
Correct |
1 ms |
1876 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
2 ms |
2372 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2248 KB |
Output is correct |
31 |
Correct |
1 ms |
2020 KB |
Output is correct |
32 |
Correct |
1 ms |
2388 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2628 KB |
Output is correct |
36 |
Correct |
1 ms |
1864 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
50 ms |
162876 KB |
Output is correct |
3 |
Correct |
50 ms |
162132 KB |
Output is correct |
4 |
Correct |
51 ms |
162916 KB |
Output is correct |
5 |
Correct |
51 ms |
162876 KB |
Output is correct |
6 |
Correct |
50 ms |
162892 KB |
Output is correct |
7 |
Correct |
59 ms |
162264 KB |
Output is correct |
8 |
Correct |
52 ms |
162164 KB |
Output is correct |
9 |
Correct |
52 ms |
161356 KB |
Output is correct |
10 |
Correct |
53 ms |
162868 KB |
Output is correct |
11 |
Correct |
52 ms |
162976 KB |
Output is correct |
12 |
Correct |
17 ms |
43984 KB |
Output is correct |
13 |
Correct |
29 ms |
77064 KB |
Output is correct |
14 |
Correct |
40 ms |
111316 KB |
Output is correct |
15 |
Correct |
57 ms |
162984 KB |
Output is correct |
16 |
Correct |
53 ms |
162904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
2244 KB |
Output is correct |
19 |
Correct |
1 ms |
1876 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
2 ms |
2372 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
1 ms |
2132 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2248 KB |
Output is correct |
31 |
Correct |
1 ms |
2020 KB |
Output is correct |
32 |
Correct |
1 ms |
2388 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2628 KB |
Output is correct |
36 |
Correct |
1 ms |
1864 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2260 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
2132 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
50 ms |
162876 KB |
Output is correct |
44 |
Correct |
50 ms |
162132 KB |
Output is correct |
45 |
Correct |
51 ms |
162916 KB |
Output is correct |
46 |
Correct |
51 ms |
162876 KB |
Output is correct |
47 |
Correct |
50 ms |
162892 KB |
Output is correct |
48 |
Correct |
59 ms |
162264 KB |
Output is correct |
49 |
Correct |
52 ms |
162164 KB |
Output is correct |
50 |
Correct |
52 ms |
161356 KB |
Output is correct |
51 |
Correct |
53 ms |
162868 KB |
Output is correct |
52 |
Correct |
52 ms |
162976 KB |
Output is correct |
53 |
Correct |
17 ms |
43984 KB |
Output is correct |
54 |
Correct |
29 ms |
77064 KB |
Output is correct |
55 |
Correct |
40 ms |
111316 KB |
Output is correct |
56 |
Correct |
57 ms |
162984 KB |
Output is correct |
57 |
Correct |
53 ms |
162904 KB |
Output is correct |
58 |
Correct |
46 ms |
76580 KB |
Output is correct |
59 |
Correct |
53 ms |
97068 KB |
Output is correct |
60 |
Correct |
52 ms |
88260 KB |
Output is correct |
61 |
Correct |
49 ms |
85268 KB |
Output is correct |
62 |
Correct |
57 ms |
160644 KB |
Output is correct |
63 |
Correct |
57 ms |
160196 KB |
Output is correct |
64 |
Correct |
65 ms |
150072 KB |
Output is correct |
65 |
Correct |
66 ms |
138576 KB |
Output is correct |
66 |
Correct |
52 ms |
86544 KB |
Output is correct |
67 |
Correct |
58 ms |
83260 KB |
Output is correct |
68 |
Correct |
61 ms |
89096 KB |
Output is correct |
69 |
Correct |
50 ms |
85892 KB |
Output is correct |
70 |
Correct |
51 ms |
92952 KB |
Output is correct |
71 |
Correct |
51 ms |
84376 KB |
Output is correct |
72 |
Correct |
22 ms |
41804 KB |
Output is correct |
73 |
Correct |
2 ms |
2900 KB |
Output is correct |