#include <bits/stdc++.h>
using namespace std;
int N;
int memo[410][410][410][3];
int rp[410],gp[410],yp[410];
int R;
int G;
int Y;
char lst[410];
const int inf = 1100100100;
bool visited[410];
int root[410][3][410];
int q(int r, int g, int y, int e){
return root[r][0][e] + root[g][1][e] + root[y][2][e];
}
int main(){
scanf(" %d",&N);
for(int i = 0; i < N; i++){
scanf(" %c",&lst[i]);
if(lst[i] == 'R'){
R++;
}
else if(lst[i] == 'G'){
G++;
}
else if(lst[i] == 'Y'){
Y++;
}
}
memo[R][G][Y][0] = 0;
memo[R][G][Y][1] = 0;
memo[R][G][Y][2] = 0;
int it = 0;
for(int i = 0; i < N; i++){
if(lst[i] == 'R'){
rp[it] = i;
it++;
}
}
it = 0;
for(int i = 0; i < N; i++){
if(lst[i] == 'G'){
gp[it] = i;
it++;
}
}
it = 0;
for(int i = 0; i < N; i++){
if(lst[i] == 'Y'){
yp[it] = i;
it++;
}
}
for(int i = 0; i <= N; i++){
root[R][0][i] = 0;
root[G][0][i] = 0;
root[Y][0][i] = 0;
}
for(int r = R - 1; r >= 0; r--){
for(int i = 0; i <= N; i++){
root[r][0][i] = root[r + 1][0][i];
if(i >= rp[r]) root[r][0][i]++;
}
}
for(int g = G - 1; g >= 0; g--){
for(int i = 0; i <= N; i++){
root[g][1][i] = root[g + 1][1][i];
if(i >= gp[g]) root[g][1][i]++;
}
}
for(int y = Y - 1; y >= 0; y--){
for(int i = 0; i <= N; i++){
root[y][2][i] = root[y + 1][2][i];
if(i >= yp[y]) root[y][2][i]++;
}
}
for(int i = N - 1; i >= 0; i--){
for(int r = 0; r <= min(R,i); r++){
for(int g = 0; g <= min(G,i - r); g++){
for(int y = 0; y <= min(Y,i - r - g); y++){
int j = r + g + y;
if(r == R || min(memo[r + 1][g][y][1], memo[r + 1][g][y][2]) == inf){
memo[r][g][y][0] = inf;
}
else{
int pos = rp[r] - q(r+1,g,y,rp[r]);
memo[r][g][y][0] = min(memo[r + 1][g][y][1], memo[r + 1][g][y][2]) + j-pos;
}
if(g == G || min(memo[r][g + 1][y][0], memo[r][g + 1][y][2]) == inf){
memo[r][g][y][1] = inf;
}
else{
int pos = gp[g] - q(r,g+1,y,gp[g]);
memo[r][g][y][1] = min(memo[r][g + 1][y][0], memo[r][g + 1][y][2])+ j-pos;
}
if(y == Y || min(memo[r][g][y + 1][0], memo[r][g][y + 1][1]) == inf){
memo[r][g][y][2] = inf;
}
else{
int pos = yp[y] - q(r,g,y+1,yp[y]);
memo[r][g][y][2] = min(memo[r][g][y + 1][0], memo[r][g][y + 1][1])+ j-pos;
}
}
}
}
}
if(min(memo[0][0][0][0],min(memo[0][0][0][1],memo[0][0][0][2])) != inf){
printf("%d",min(memo[0][0][0][0],min(memo[0][0][0][1],memo[0][0][0][2])));
}
else{
printf("-1");
}
}
/*
6
GGGGRRR
GRGRGRG
RGRGYG
*/
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:21:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf(" %d",&N);
| ~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:24:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | scanf(" %c",&lst[i]);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
4 ms |
6492 KB |
Output is correct |
19 |
Correct |
4 ms |
5976 KB |
Output is correct |
20 |
Correct |
4 ms |
6232 KB |
Output is correct |
21 |
Correct |
4 ms |
6492 KB |
Output is correct |
22 |
Correct |
4 ms |
5720 KB |
Output is correct |
23 |
Correct |
3 ms |
6236 KB |
Output is correct |
24 |
Correct |
3 ms |
5724 KB |
Output is correct |
25 |
Correct |
3 ms |
8024 KB |
Output is correct |
26 |
Correct |
3 ms |
8284 KB |
Output is correct |
27 |
Correct |
4 ms |
7260 KB |
Output is correct |
28 |
Correct |
4 ms |
6236 KB |
Output is correct |
29 |
Correct |
4 ms |
6236 KB |
Output is correct |
30 |
Correct |
4 ms |
6236 KB |
Output is correct |
31 |
Correct |
3 ms |
5980 KB |
Output is correct |
32 |
Correct |
4 ms |
6492 KB |
Output is correct |
33 |
Correct |
3 ms |
7776 KB |
Output is correct |
34 |
Correct |
3 ms |
7516 KB |
Output is correct |
35 |
Correct |
3 ms |
6744 KB |
Output is correct |
36 |
Correct |
3 ms |
5980 KB |
Output is correct |
37 |
Correct |
3 ms |
5772 KB |
Output is correct |
38 |
Correct |
3 ms |
6236 KB |
Output is correct |
39 |
Correct |
4 ms |
6468 KB |
Output is correct |
40 |
Correct |
1 ms |
2652 KB |
Output is correct |
41 |
Correct |
2 ms |
6236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
169 ms |
166228 KB |
Output is correct |
3 |
Correct |
139 ms |
165460 KB |
Output is correct |
4 |
Correct |
142 ms |
166252 KB |
Output is correct |
5 |
Correct |
138 ms |
166228 KB |
Output is correct |
6 |
Correct |
147 ms |
166612 KB |
Output is correct |
7 |
Correct |
140 ms |
165456 KB |
Output is correct |
8 |
Correct |
139 ms |
165440 KB |
Output is correct |
9 |
Correct |
135 ms |
164688 KB |
Output is correct |
10 |
Correct |
136 ms |
166480 KB |
Output is correct |
11 |
Correct |
140 ms |
166216 KB |
Output is correct |
12 |
Correct |
33 ms |
47708 KB |
Output is correct |
13 |
Correct |
55 ms |
80720 KB |
Output is correct |
14 |
Correct |
91 ms |
115028 KB |
Output is correct |
15 |
Correct |
146 ms |
166140 KB |
Output is correct |
16 |
Correct |
171 ms |
166356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
2 ms |
4700 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4700 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
4 ms |
6492 KB |
Output is correct |
19 |
Correct |
4 ms |
5976 KB |
Output is correct |
20 |
Correct |
4 ms |
6232 KB |
Output is correct |
21 |
Correct |
4 ms |
6492 KB |
Output is correct |
22 |
Correct |
4 ms |
5720 KB |
Output is correct |
23 |
Correct |
3 ms |
6236 KB |
Output is correct |
24 |
Correct |
3 ms |
5724 KB |
Output is correct |
25 |
Correct |
3 ms |
8024 KB |
Output is correct |
26 |
Correct |
3 ms |
8284 KB |
Output is correct |
27 |
Correct |
4 ms |
7260 KB |
Output is correct |
28 |
Correct |
4 ms |
6236 KB |
Output is correct |
29 |
Correct |
4 ms |
6236 KB |
Output is correct |
30 |
Correct |
4 ms |
6236 KB |
Output is correct |
31 |
Correct |
3 ms |
5980 KB |
Output is correct |
32 |
Correct |
4 ms |
6492 KB |
Output is correct |
33 |
Correct |
3 ms |
7776 KB |
Output is correct |
34 |
Correct |
3 ms |
7516 KB |
Output is correct |
35 |
Correct |
3 ms |
6744 KB |
Output is correct |
36 |
Correct |
3 ms |
5980 KB |
Output is correct |
37 |
Correct |
3 ms |
5772 KB |
Output is correct |
38 |
Correct |
3 ms |
6236 KB |
Output is correct |
39 |
Correct |
4 ms |
6468 KB |
Output is correct |
40 |
Correct |
1 ms |
2652 KB |
Output is correct |
41 |
Correct |
2 ms |
6236 KB |
Output is correct |
42 |
Correct |
1 ms |
4444 KB |
Output is correct |
43 |
Correct |
169 ms |
166228 KB |
Output is correct |
44 |
Correct |
139 ms |
165460 KB |
Output is correct |
45 |
Correct |
142 ms |
166252 KB |
Output is correct |
46 |
Correct |
138 ms |
166228 KB |
Output is correct |
47 |
Correct |
147 ms |
166612 KB |
Output is correct |
48 |
Correct |
140 ms |
165456 KB |
Output is correct |
49 |
Correct |
139 ms |
165440 KB |
Output is correct |
50 |
Correct |
135 ms |
164688 KB |
Output is correct |
51 |
Correct |
136 ms |
166480 KB |
Output is correct |
52 |
Correct |
140 ms |
166216 KB |
Output is correct |
53 |
Correct |
33 ms |
47708 KB |
Output is correct |
54 |
Correct |
55 ms |
80720 KB |
Output is correct |
55 |
Correct |
91 ms |
115028 KB |
Output is correct |
56 |
Correct |
146 ms |
166140 KB |
Output is correct |
57 |
Correct |
171 ms |
166356 KB |
Output is correct |
58 |
Execution timed out |
1062 ms |
80468 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |