#include <iostream>
#include <cstring>
using namespace std;
const int N = 405;
const int INF = N * N;
#define offset(i) (ro[rx][i] + go[gx][i] + yo[yx][i])
int n, rc = 0, gc = 0, yc = 0;
string s;
int ri[N], gi[N], yi[N];
int ro[N][N], go[N][N], yo[N][N];
int dp[N][N][N][3];
void precomp() {
for (int i = 0; i < n; i++) {
if (s[i] == 'R') {
ri[rc] = i;
rc++;
} else if (s[i] == 'G') {
gi[gc] = i;
gc++;
} else {
yi[yc] = i;
yc++;
}
for (int j = i - 1; j >= 0; j--) {
if (s[i] == 'R') ro[rc][j] = 1 + ro[rc - 1][j];
else if (s[i] == 'G') go[gc][j] = 1 + go[gc - 1][j];
else yo[yc][j] = 1 + yo[yc - 1][j];
}
}
for (int i = 0; i <= rc; i++) {
for (int j = 0; j <= gc; j++) {
for (int k = 0; k <= yc; k++) {
dp[i][j][k][0] = dp[i][j][k][1] = dp[i][j][k][2] = -1;
}
}
}
}
int solve(int rx, int gx, int yx, int last) {
if (rx == rc and gx == gc and yx == yc) return 0;
if (dp[rx][gx][yx][last] != -1) return dp[rx][gx][yx][last];
int &ret = dp[rx][gx][yx][last];
int len = rx + gx + yx;
int i = ri[rx];
ret = INF;
if (last != 0 and rx < rc) ret = min(ret, i + ro[rx][i] + go[gx][i] + yo[yx][i] - len + solve(rx + 1, gx, yx, 0));
i = gi[gx];
if (last != 1 and gx < gc) ret = min(ret, i + ro[rx][i] + go[gx][i] + yo[yx][i] - len + solve(rx, gx + 1, yx, 1));
i = yi[yx];
if (last != 2 and yx < yc) ret = min(ret, i + ro[rx][i] + go[gx][i] + yo[yx][i] - len + solve(rx, gx, yx + 1, 2));
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> s;
precomp();
int ans = min(solve(0, 0, 0, 0), solve(0, 0, 0, 1));
cout << (ans >= INF ? -1 : ans) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
456 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 |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
456 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 |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
2388 KB |
Output is correct |
19 |
Correct |
1 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2316 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
1748 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 |
4052 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3156 KB |
Output is correct |
28 |
Correct |
1 ms |
2388 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2380 KB |
Output is correct |
31 |
Correct |
1 ms |
2116 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
2 ms |
3924 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
1992 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
3 ms |
2388 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
2260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
57 ms |
163524 KB |
Output is correct |
3 |
Correct |
63 ms |
162744 KB |
Output is correct |
4 |
Correct |
57 ms |
163580 KB |
Output is correct |
5 |
Correct |
54 ms |
163596 KB |
Output is correct |
6 |
Correct |
58 ms |
163600 KB |
Output is correct |
7 |
Correct |
58 ms |
162876 KB |
Output is correct |
8 |
Correct |
57 ms |
162724 KB |
Output is correct |
9 |
Correct |
61 ms |
161992 KB |
Output is correct |
10 |
Correct |
62 ms |
163532 KB |
Output is correct |
11 |
Correct |
91 ms |
163536 KB |
Output is correct |
12 |
Correct |
17 ms |
44372 KB |
Output is correct |
13 |
Correct |
29 ms |
77524 KB |
Output is correct |
14 |
Correct |
43 ms |
111844 KB |
Output is correct |
15 |
Correct |
56 ms |
163632 KB |
Output is correct |
16 |
Correct |
57 ms |
163620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
456 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 |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
2388 KB |
Output is correct |
19 |
Correct |
1 ms |
2004 KB |
Output is correct |
20 |
Correct |
2 ms |
2316 KB |
Output is correct |
21 |
Correct |
1 ms |
2516 KB |
Output is correct |
22 |
Correct |
2 ms |
1748 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 |
4052 KB |
Output is correct |
26 |
Correct |
2 ms |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3156 KB |
Output is correct |
28 |
Correct |
1 ms |
2388 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2380 KB |
Output is correct |
31 |
Correct |
1 ms |
2116 KB |
Output is correct |
32 |
Correct |
2 ms |
2516 KB |
Output is correct |
33 |
Correct |
2 ms |
3924 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
1992 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
3 ms |
2388 KB |
Output is correct |
40 |
Correct |
1 ms |
468 KB |
Output is correct |
41 |
Correct |
1 ms |
2260 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
57 ms |
163524 KB |
Output is correct |
44 |
Correct |
63 ms |
162744 KB |
Output is correct |
45 |
Correct |
57 ms |
163580 KB |
Output is correct |
46 |
Correct |
54 ms |
163596 KB |
Output is correct |
47 |
Correct |
58 ms |
163600 KB |
Output is correct |
48 |
Correct |
58 ms |
162876 KB |
Output is correct |
49 |
Correct |
57 ms |
162724 KB |
Output is correct |
50 |
Correct |
61 ms |
161992 KB |
Output is correct |
51 |
Correct |
62 ms |
163532 KB |
Output is correct |
52 |
Correct |
91 ms |
163536 KB |
Output is correct |
53 |
Correct |
17 ms |
44372 KB |
Output is correct |
54 |
Correct |
29 ms |
77524 KB |
Output is correct |
55 |
Correct |
43 ms |
111844 KB |
Output is correct |
56 |
Correct |
56 ms |
163632 KB |
Output is correct |
57 |
Correct |
57 ms |
163620 KB |
Output is correct |
58 |
Correct |
125 ms |
76876 KB |
Output is correct |
59 |
Correct |
129 ms |
97296 KB |
Output is correct |
60 |
Correct |
133 ms |
88500 KB |
Output is correct |
61 |
Correct |
136 ms |
85464 KB |
Output is correct |
62 |
Correct |
59 ms |
161228 KB |
Output is correct |
63 |
Correct |
57 ms |
160872 KB |
Output is correct |
64 |
Correct |
67 ms |
150764 KB |
Output is correct |
65 |
Correct |
84 ms |
139116 KB |
Output is correct |
66 |
Correct |
129 ms |
86776 KB |
Output is correct |
67 |
Correct |
119 ms |
83560 KB |
Output is correct |
68 |
Correct |
129 ms |
89376 KB |
Output is correct |
69 |
Correct |
136 ms |
86132 KB |
Output is correct |
70 |
Correct |
156 ms |
93260 KB |
Output is correct |
71 |
Correct |
131 ms |
84668 KB |
Output is correct |
72 |
Correct |
24 ms |
42196 KB |
Output is correct |
73 |
Correct |
2 ms |
3540 KB |
Output is correct |