#include <bits/stdc++.h>
#define RED 0
#define GRE 1
#define YEL 2
using namespace std;
const int inf = 1e9;
int N;
int f[205][205][205][3];
int pos[405][3], pre[405][3];
int pr, py, pg;
int calc(int r, int g, int y, int last)
{
if(r == pr && g == pg && y == py)
return 0;
if(f[r][g][y][last] != -1)
return f[r][g][y][last];
int res = inf;
if(r < pr && last != 0) {
int now = pos[r][0];
res = min(res, calc(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, calc(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, calc(r, g, y + 1, 2) + max(pre[now][0] - r, 0) + max(pre[now][1] - g, 0));
}
return f[r][g][y][last] = res;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(); cout.tie();
cin >> N;
char c;
memset(f, -1, sizeof(f));
for(int i = 1; i <= N; i++) {
cin >> c;
if(c == 'R') {
pre[i][0] = 1;
pos[pr++][0] = i;
}
else if(c == '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 << endl;
return 0;
}
int ans = inf;
if(pr > 0)
{
int now = pos[0][0];
ans = min(ans, calc(1, 0, 0, 0) + pre[now][1] + pre[now][2]);
}
if(pg > 0)
{
int now = pos[0][1];
ans = min(ans, calc(0, 1, 0, 1) + pre[now][0] + pre[now][2]);
}
if(py > 0)
{
int now = pos[0][2];
ans = min(ans, calc(0, 0, 1, 2) + pre[now][0] + pre[now][1]);
}
if(ans != inf) {
cout << ans << endl;
}
else
cout << endl;
cin >> N;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
101496 KB |
Output is correct |
2 |
Correct |
56 ms |
101496 KB |
Output is correct |
3 |
Correct |
57 ms |
101504 KB |
Output is correct |
4 |
Correct |
58 ms |
101496 KB |
Output is correct |
5 |
Correct |
55 ms |
101496 KB |
Output is correct |
6 |
Correct |
57 ms |
101504 KB |
Output is correct |
7 |
Correct |
56 ms |
101496 KB |
Output is correct |
8 |
Correct |
55 ms |
101496 KB |
Output is correct |
9 |
Correct |
59 ms |
101496 KB |
Output is correct |
10 |
Correct |
56 ms |
101496 KB |
Output is correct |
11 |
Correct |
56 ms |
101496 KB |
Output is correct |
12 |
Correct |
55 ms |
101496 KB |
Output is correct |
13 |
Correct |
55 ms |
101504 KB |
Output is correct |
14 |
Correct |
56 ms |
101496 KB |
Output is correct |
15 |
Correct |
58 ms |
101496 KB |
Output is correct |
16 |
Correct |
56 ms |
101496 KB |
Output is correct |
17 |
Correct |
56 ms |
101496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
101496 KB |
Output is correct |
2 |
Correct |
56 ms |
101496 KB |
Output is correct |
3 |
Correct |
57 ms |
101504 KB |
Output is correct |
4 |
Correct |
58 ms |
101496 KB |
Output is correct |
5 |
Correct |
55 ms |
101496 KB |
Output is correct |
6 |
Correct |
57 ms |
101504 KB |
Output is correct |
7 |
Correct |
56 ms |
101496 KB |
Output is correct |
8 |
Correct |
55 ms |
101496 KB |
Output is correct |
9 |
Correct |
59 ms |
101496 KB |
Output is correct |
10 |
Correct |
56 ms |
101496 KB |
Output is correct |
11 |
Correct |
56 ms |
101496 KB |
Output is correct |
12 |
Correct |
55 ms |
101496 KB |
Output is correct |
13 |
Correct |
55 ms |
101504 KB |
Output is correct |
14 |
Correct |
56 ms |
101496 KB |
Output is correct |
15 |
Correct |
58 ms |
101496 KB |
Output is correct |
16 |
Correct |
56 ms |
101496 KB |
Output is correct |
17 |
Correct |
56 ms |
101496 KB |
Output is correct |
18 |
Correct |
56 ms |
101452 KB |
Output is correct |
19 |
Correct |
57 ms |
101496 KB |
Output is correct |
20 |
Correct |
57 ms |
101496 KB |
Output is correct |
21 |
Correct |
58 ms |
101500 KB |
Output is correct |
22 |
Correct |
59 ms |
101496 KB |
Output is correct |
23 |
Correct |
56 ms |
101496 KB |
Output is correct |
24 |
Correct |
56 ms |
101496 KB |
Output is correct |
25 |
Correct |
57 ms |
101496 KB |
Output is correct |
26 |
Correct |
55 ms |
101496 KB |
Output is correct |
27 |
Correct |
58 ms |
101496 KB |
Output is correct |
28 |
Correct |
57 ms |
101496 KB |
Output is correct |
29 |
Correct |
56 ms |
101496 KB |
Output is correct |
30 |
Correct |
57 ms |
101496 KB |
Output is correct |
31 |
Correct |
57 ms |
101496 KB |
Output is correct |
32 |
Correct |
56 ms |
101496 KB |
Output is correct |
33 |
Correct |
55 ms |
101496 KB |
Output is correct |
34 |
Correct |
55 ms |
101496 KB |
Output is correct |
35 |
Correct |
62 ms |
101496 KB |
Output is correct |
36 |
Correct |
57 ms |
101496 KB |
Output is correct |
37 |
Correct |
56 ms |
101496 KB |
Output is correct |
38 |
Correct |
56 ms |
101496 KB |
Output is correct |
39 |
Correct |
57 ms |
101624 KB |
Output is correct |
40 |
Correct |
57 ms |
101624 KB |
Output is correct |
41 |
Correct |
55 ms |
101496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
101508 KB |
Output is correct |
2 |
Correct |
56 ms |
101480 KB |
Output is correct |
3 |
Correct |
57 ms |
101496 KB |
Output is correct |
4 |
Correct |
57 ms |
101500 KB |
Output is correct |
5 |
Correct |
56 ms |
101496 KB |
Output is correct |
6 |
Correct |
58 ms |
101496 KB |
Output is correct |
7 |
Correct |
56 ms |
101496 KB |
Output is correct |
8 |
Correct |
57 ms |
101496 KB |
Output is correct |
9 |
Correct |
58 ms |
101500 KB |
Output is correct |
10 |
Correct |
58 ms |
101496 KB |
Output is correct |
11 |
Correct |
56 ms |
101496 KB |
Output is correct |
12 |
Correct |
55 ms |
101496 KB |
Output is correct |
13 |
Correct |
55 ms |
101496 KB |
Output is correct |
14 |
Correct |
55 ms |
101496 KB |
Output is correct |
15 |
Correct |
57 ms |
101624 KB |
Output is correct |
16 |
Correct |
57 ms |
101496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
101496 KB |
Output is correct |
2 |
Correct |
56 ms |
101496 KB |
Output is correct |
3 |
Correct |
57 ms |
101504 KB |
Output is correct |
4 |
Correct |
58 ms |
101496 KB |
Output is correct |
5 |
Correct |
55 ms |
101496 KB |
Output is correct |
6 |
Correct |
57 ms |
101504 KB |
Output is correct |
7 |
Correct |
56 ms |
101496 KB |
Output is correct |
8 |
Correct |
55 ms |
101496 KB |
Output is correct |
9 |
Correct |
59 ms |
101496 KB |
Output is correct |
10 |
Correct |
56 ms |
101496 KB |
Output is correct |
11 |
Correct |
56 ms |
101496 KB |
Output is correct |
12 |
Correct |
55 ms |
101496 KB |
Output is correct |
13 |
Correct |
55 ms |
101504 KB |
Output is correct |
14 |
Correct |
56 ms |
101496 KB |
Output is correct |
15 |
Correct |
58 ms |
101496 KB |
Output is correct |
16 |
Correct |
56 ms |
101496 KB |
Output is correct |
17 |
Correct |
56 ms |
101496 KB |
Output is correct |
18 |
Correct |
56 ms |
101452 KB |
Output is correct |
19 |
Correct |
57 ms |
101496 KB |
Output is correct |
20 |
Correct |
57 ms |
101496 KB |
Output is correct |
21 |
Correct |
58 ms |
101500 KB |
Output is correct |
22 |
Correct |
59 ms |
101496 KB |
Output is correct |
23 |
Correct |
56 ms |
101496 KB |
Output is correct |
24 |
Correct |
56 ms |
101496 KB |
Output is correct |
25 |
Correct |
57 ms |
101496 KB |
Output is correct |
26 |
Correct |
55 ms |
101496 KB |
Output is correct |
27 |
Correct |
58 ms |
101496 KB |
Output is correct |
28 |
Correct |
57 ms |
101496 KB |
Output is correct |
29 |
Correct |
56 ms |
101496 KB |
Output is correct |
30 |
Correct |
57 ms |
101496 KB |
Output is correct |
31 |
Correct |
57 ms |
101496 KB |
Output is correct |
32 |
Correct |
56 ms |
101496 KB |
Output is correct |
33 |
Correct |
55 ms |
101496 KB |
Output is correct |
34 |
Correct |
55 ms |
101496 KB |
Output is correct |
35 |
Correct |
62 ms |
101496 KB |
Output is correct |
36 |
Correct |
57 ms |
101496 KB |
Output is correct |
37 |
Correct |
56 ms |
101496 KB |
Output is correct |
38 |
Correct |
56 ms |
101496 KB |
Output is correct |
39 |
Correct |
57 ms |
101624 KB |
Output is correct |
40 |
Correct |
57 ms |
101624 KB |
Output is correct |
41 |
Correct |
55 ms |
101496 KB |
Output is correct |
42 |
Correct |
56 ms |
101508 KB |
Output is correct |
43 |
Correct |
56 ms |
101480 KB |
Output is correct |
44 |
Correct |
57 ms |
101496 KB |
Output is correct |
45 |
Correct |
57 ms |
101500 KB |
Output is correct |
46 |
Correct |
56 ms |
101496 KB |
Output is correct |
47 |
Correct |
58 ms |
101496 KB |
Output is correct |
48 |
Correct |
56 ms |
101496 KB |
Output is correct |
49 |
Correct |
57 ms |
101496 KB |
Output is correct |
50 |
Correct |
58 ms |
101500 KB |
Output is correct |
51 |
Correct |
58 ms |
101496 KB |
Output is correct |
52 |
Correct |
56 ms |
101496 KB |
Output is correct |
53 |
Correct |
55 ms |
101496 KB |
Output is correct |
54 |
Correct |
55 ms |
101496 KB |
Output is correct |
55 |
Correct |
55 ms |
101496 KB |
Output is correct |
56 |
Correct |
57 ms |
101624 KB |
Output is correct |
57 |
Correct |
57 ms |
101496 KB |
Output is correct |
58 |
Correct |
166 ms |
101496 KB |
Output is correct |
59 |
Correct |
170 ms |
101496 KB |
Output is correct |
60 |
Correct |
171 ms |
101496 KB |
Output is correct |
61 |
Correct |
174 ms |
101496 KB |
Output is correct |
62 |
Correct |
57 ms |
101496 KB |
Output is correct |
63 |
Correct |
58 ms |
101496 KB |
Output is correct |
64 |
Correct |
73 ms |
101624 KB |
Output is correct |
65 |
Correct |
98 ms |
101496 KB |
Output is correct |
66 |
Correct |
168 ms |
101496 KB |
Output is correct |
67 |
Correct |
169 ms |
101496 KB |
Output is correct |
68 |
Correct |
172 ms |
101496 KB |
Output is correct |
69 |
Correct |
182 ms |
101496 KB |
Output is correct |
70 |
Correct |
171 ms |
101496 KB |
Output is correct |
71 |
Correct |
169 ms |
101496 KB |
Output is correct |
72 |
Correct |
57 ms |
101496 KB |
Output is correct |
73 |
Correct |
55 ms |
101528 KB |
Output is correct |