#include <bits/stdc++.h>
#define RED 0
#define GRE 1
#define YEL 2
using namespace std;
int N;
int memo[205][205][205][3];
int pos[405][3], pre[405][3];
int pr, py, pg;
int dp(int r, int g, int y, int last) {
if(r == pr && g == pg && y == py) return 0;
if(memo[r][g][y][last] != -1) return memo[r][g][y][last];
int res = 1000000000;
if(r < pr && last != 0) {
int now = pos[r][0];
res = min(res, dp(r + 1, g, y, 0) + max(pre[now][1] - g, 0) + max(pre[now][2] - y, 0));
}
if(g < pg && last != 1) {
int now = pos[g][1];
res = min(res, dp(r, g + 1, y, 1) + max(pre[now][0] - r, 0) + max(pre[now][2] - y, 0));
}
if(y < py && last != 2) {
int now = pos[y][2];
res = min(res, dp(r, g, y + 1, 2) + max(pre[now][0] - r, 0) + max(pre[now][1] - g, 0));
}
return memo[r][g][y][last] = res;
}
int main() {
memset(memo, -1, sizeof(memo));
ios_base::sync_with_stdio(0);
cin >> N;
char ch;
for(int i = 1; i <= N; i++) {
cin >> ch;
if(ch == 'R') {
pre[i][0] = 1;
pos[pr++][0] = i;
}else if(ch == 'G') {
pre[i][1] = 1;
pos[pg++][1] = i;
}else {
pre[i][2] = 1;
pos[py++][2] = i;
}
pre[i][0] += pre[i - 1][0];
pre[i][1] += pre[i - 1][1];
pre[i][2] += pre[i - 1][2];
}
if(max(pr, max(pg, py)) > pr + py + pg - max(pr, max(pg, py)) + 1) {
cout << "-1\n";
return 0;
}
int ans = 1000000000;
if(pr > 0) {
int now = pos[0][0];
ans = min(ans, dp(1, 0, 0, 0) + pre[now][1] + pre[now][2]);
}
if(pg > 0) {
int now = pos[0][1];
ans = min(ans, dp(0, 1, 0, 1) + pre[now][0] + pre[now][2]);
}
if(py > 0) {
int now = pos[0][2];
ans = min(ans, dp(0, 0, 1, 2) + pre[now][0] + pre[now][1]);
}
if(ans != 1000000000) {
cout << ans << "\n";
}else cout << "-1\n";
cin >> N;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
101496 KB |
Output is correct |
2 |
Correct |
55 ms |
101496 KB |
Output is correct |
3 |
Correct |
58 ms |
101496 KB |
Output is correct |
4 |
Correct |
55 ms |
101496 KB |
Output is correct |
5 |
Correct |
56 ms |
101496 KB |
Output is correct |
6 |
Correct |
55 ms |
101496 KB |
Output is correct |
7 |
Correct |
66 ms |
101496 KB |
Output is correct |
8 |
Correct |
55 ms |
101496 KB |
Output is correct |
9 |
Correct |
58 ms |
101496 KB |
Output is correct |
10 |
Correct |
57 ms |
101496 KB |
Output is correct |
11 |
Correct |
56 ms |
101500 KB |
Output is correct |
12 |
Correct |
58 ms |
101496 KB |
Output is correct |
13 |
Correct |
67 ms |
101500 KB |
Output is correct |
14 |
Correct |
58 ms |
101496 KB |
Output is correct |
15 |
Correct |
56 ms |
101496 KB |
Output is correct |
16 |
Correct |
55 ms |
101496 KB |
Output is correct |
17 |
Correct |
55 ms |
101496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
101496 KB |
Output is correct |
2 |
Correct |
55 ms |
101496 KB |
Output is correct |
3 |
Correct |
58 ms |
101496 KB |
Output is correct |
4 |
Correct |
55 ms |
101496 KB |
Output is correct |
5 |
Correct |
56 ms |
101496 KB |
Output is correct |
6 |
Correct |
55 ms |
101496 KB |
Output is correct |
7 |
Correct |
66 ms |
101496 KB |
Output is correct |
8 |
Correct |
55 ms |
101496 KB |
Output is correct |
9 |
Correct |
58 ms |
101496 KB |
Output is correct |
10 |
Correct |
57 ms |
101496 KB |
Output is correct |
11 |
Correct |
56 ms |
101500 KB |
Output is correct |
12 |
Correct |
58 ms |
101496 KB |
Output is correct |
13 |
Correct |
67 ms |
101500 KB |
Output is correct |
14 |
Correct |
58 ms |
101496 KB |
Output is correct |
15 |
Correct |
56 ms |
101496 KB |
Output is correct |
16 |
Correct |
55 ms |
101496 KB |
Output is correct |
17 |
Correct |
55 ms |
101496 KB |
Output is correct |
18 |
Correct |
56 ms |
101496 KB |
Output is correct |
19 |
Correct |
64 ms |
101496 KB |
Output is correct |
20 |
Correct |
57 ms |
101496 KB |
Output is correct |
21 |
Correct |
57 ms |
101496 KB |
Output is correct |
22 |
Correct |
58 ms |
101496 KB |
Output is correct |
23 |
Correct |
55 ms |
101500 KB |
Output is correct |
24 |
Correct |
57 ms |
101420 KB |
Output is correct |
25 |
Correct |
58 ms |
101496 KB |
Output is correct |
26 |
Correct |
57 ms |
101496 KB |
Output is correct |
27 |
Correct |
56 ms |
101496 KB |
Output is correct |
28 |
Correct |
58 ms |
101496 KB |
Output is correct |
29 |
Correct |
65 ms |
101496 KB |
Output is correct |
30 |
Correct |
56 ms |
101496 KB |
Output is correct |
31 |
Correct |
56 ms |
101496 KB |
Output is correct |
32 |
Correct |
58 ms |
101496 KB |
Output is correct |
33 |
Correct |
55 ms |
101496 KB |
Output is correct |
34 |
Correct |
58 ms |
101496 KB |
Output is correct |
35 |
Correct |
57 ms |
101496 KB |
Output is correct |
36 |
Correct |
56 ms |
101496 KB |
Output is correct |
37 |
Correct |
62 ms |
101624 KB |
Output is correct |
38 |
Correct |
57 ms |
101496 KB |
Output is correct |
39 |
Correct |
56 ms |
101496 KB |
Output is correct |
40 |
Correct |
66 ms |
101496 KB |
Output is correct |
41 |
Correct |
56 ms |
101496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
101496 KB |
Output is correct |
2 |
Correct |
56 ms |
101496 KB |
Output is correct |
3 |
Correct |
59 ms |
101496 KB |
Output is correct |
4 |
Correct |
65 ms |
101496 KB |
Output is correct |
5 |
Correct |
56 ms |
101496 KB |
Output is correct |
6 |
Correct |
55 ms |
101496 KB |
Output is correct |
7 |
Correct |
57 ms |
101496 KB |
Output is correct |
8 |
Correct |
64 ms |
101496 KB |
Output is correct |
9 |
Correct |
56 ms |
101496 KB |
Output is correct |
10 |
Correct |
56 ms |
101496 KB |
Output is correct |
11 |
Correct |
55 ms |
101496 KB |
Output is correct |
12 |
Correct |
55 ms |
101496 KB |
Output is correct |
13 |
Correct |
56 ms |
101496 KB |
Output is correct |
14 |
Correct |
56 ms |
101496 KB |
Output is correct |
15 |
Correct |
56 ms |
101496 KB |
Output is correct |
16 |
Correct |
56 ms |
101496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
101496 KB |
Output is correct |
2 |
Correct |
55 ms |
101496 KB |
Output is correct |
3 |
Correct |
58 ms |
101496 KB |
Output is correct |
4 |
Correct |
55 ms |
101496 KB |
Output is correct |
5 |
Correct |
56 ms |
101496 KB |
Output is correct |
6 |
Correct |
55 ms |
101496 KB |
Output is correct |
7 |
Correct |
66 ms |
101496 KB |
Output is correct |
8 |
Correct |
55 ms |
101496 KB |
Output is correct |
9 |
Correct |
58 ms |
101496 KB |
Output is correct |
10 |
Correct |
57 ms |
101496 KB |
Output is correct |
11 |
Correct |
56 ms |
101500 KB |
Output is correct |
12 |
Correct |
58 ms |
101496 KB |
Output is correct |
13 |
Correct |
67 ms |
101500 KB |
Output is correct |
14 |
Correct |
58 ms |
101496 KB |
Output is correct |
15 |
Correct |
56 ms |
101496 KB |
Output is correct |
16 |
Correct |
55 ms |
101496 KB |
Output is correct |
17 |
Correct |
55 ms |
101496 KB |
Output is correct |
18 |
Correct |
56 ms |
101496 KB |
Output is correct |
19 |
Correct |
64 ms |
101496 KB |
Output is correct |
20 |
Correct |
57 ms |
101496 KB |
Output is correct |
21 |
Correct |
57 ms |
101496 KB |
Output is correct |
22 |
Correct |
58 ms |
101496 KB |
Output is correct |
23 |
Correct |
55 ms |
101500 KB |
Output is correct |
24 |
Correct |
57 ms |
101420 KB |
Output is correct |
25 |
Correct |
58 ms |
101496 KB |
Output is correct |
26 |
Correct |
57 ms |
101496 KB |
Output is correct |
27 |
Correct |
56 ms |
101496 KB |
Output is correct |
28 |
Correct |
58 ms |
101496 KB |
Output is correct |
29 |
Correct |
65 ms |
101496 KB |
Output is correct |
30 |
Correct |
56 ms |
101496 KB |
Output is correct |
31 |
Correct |
56 ms |
101496 KB |
Output is correct |
32 |
Correct |
58 ms |
101496 KB |
Output is correct |
33 |
Correct |
55 ms |
101496 KB |
Output is correct |
34 |
Correct |
58 ms |
101496 KB |
Output is correct |
35 |
Correct |
57 ms |
101496 KB |
Output is correct |
36 |
Correct |
56 ms |
101496 KB |
Output is correct |
37 |
Correct |
62 ms |
101624 KB |
Output is correct |
38 |
Correct |
57 ms |
101496 KB |
Output is correct |
39 |
Correct |
56 ms |
101496 KB |
Output is correct |
40 |
Correct |
66 ms |
101496 KB |
Output is correct |
41 |
Correct |
56 ms |
101496 KB |
Output is correct |
42 |
Correct |
67 ms |
101496 KB |
Output is correct |
43 |
Correct |
56 ms |
101496 KB |
Output is correct |
44 |
Correct |
59 ms |
101496 KB |
Output is correct |
45 |
Correct |
65 ms |
101496 KB |
Output is correct |
46 |
Correct |
56 ms |
101496 KB |
Output is correct |
47 |
Correct |
55 ms |
101496 KB |
Output is correct |
48 |
Correct |
57 ms |
101496 KB |
Output is correct |
49 |
Correct |
64 ms |
101496 KB |
Output is correct |
50 |
Correct |
56 ms |
101496 KB |
Output is correct |
51 |
Correct |
56 ms |
101496 KB |
Output is correct |
52 |
Correct |
55 ms |
101496 KB |
Output is correct |
53 |
Correct |
55 ms |
101496 KB |
Output is correct |
54 |
Correct |
56 ms |
101496 KB |
Output is correct |
55 |
Correct |
56 ms |
101496 KB |
Output is correct |
56 |
Correct |
56 ms |
101496 KB |
Output is correct |
57 |
Correct |
56 ms |
101496 KB |
Output is correct |
58 |
Correct |
153 ms |
101496 KB |
Output is correct |
59 |
Correct |
162 ms |
101588 KB |
Output is correct |
60 |
Correct |
161 ms |
101496 KB |
Output is correct |
61 |
Correct |
163 ms |
101624 KB |
Output is correct |
62 |
Correct |
64 ms |
101496 KB |
Output is correct |
63 |
Correct |
65 ms |
101496 KB |
Output is correct |
64 |
Correct |
82 ms |
101624 KB |
Output is correct |
65 |
Correct |
89 ms |
101624 KB |
Output is correct |
66 |
Correct |
150 ms |
101496 KB |
Output is correct |
67 |
Correct |
165 ms |
101496 KB |
Output is correct |
68 |
Correct |
167 ms |
101624 KB |
Output is correct |
69 |
Correct |
153 ms |
101496 KB |
Output is correct |
70 |
Correct |
149 ms |
101496 KB |
Output is correct |
71 |
Correct |
148 ms |
101496 KB |
Output is correct |
72 |
Correct |
66 ms |
101496 KB |
Output is correct |
73 |
Correct |
55 ms |
101496 KB |
Output is correct |