#include <bits/stdc++.h>
using namespace std;
int N;
int memo[3][410][410][410];
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[3][410][410];
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[0][R][G][Y] = 0;
memo[1][R][G][Y] = 0;
memo[2][R][G][Y] = 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[0][R][i] = 0;
root[0][G][i] = 0;
root[0][Y][i] = 0;
}
for(int r = R - 1; r >= 0; r--){
for(int i = 0; i <= N; i++){
root[0][r][i] = root[0][r + 1][i];
if(i >= rp[r]) root[0][r][i]++;
}
}
for(int g = G - 1; g >= 0; g--){
for(int i = 0; i <= N; i++){
root[1][g][i] = root[1][g + 1][i];
if(i >= gp[g]) root[1][g][i]++;
}
}
for(int y = Y - 1; y >= 0; y--){
for(int i = 0; i <= N; i++){
root[2][y][i] = root[2][y + 1][i];
if(i >= yp[y]) root[2][y][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++){
int y = i - r - g;
int j = r + g + y;
if(r == R || min(memo[1][r + 1][g][y], memo[2][r + 1][g][y]) == inf){
memo[0][r][g][y] = inf;
}
else{
memo[0][r][g][y] = min(memo[1][r + 1][g][y], memo[2][r + 1][g][y]) + j-rp[r]+root[0][r+1][rp[r]] + root[1][g][rp[r]] + root[2][y][rp[r]];
}
if(g == G || min(memo[0][r][g + 1][y], memo[2][r][g + 1][y]) == inf){
memo[1][r][g][y] = inf;
}
else{
memo[1][r][g][y] = min(memo[0][r][g + 1][y], memo[2][r][g + 1][y])+ j-gp[g]+root[0][r][gp[g]] + root[1][g+1][gp[g]] + root[2][y][gp[g]];
}
if(y == Y || min(memo[0][r][g][y + 1], memo[1][r][g][y + 1]) == inf){
memo[2][r][g][y] = inf;
}
else{
memo[2][r][g][y] = min(memo[0][r][g][y + 1], memo[1][r][g][y + 1])+ j-yp[y]+root[0][r][yp[y]] + root[1][g][yp[y]] + root[2][y+1][yp[y]];
}
}
}
}
if(min(memo[0][0][0][0],min(memo[1][0][0][0],memo[2][0][0][0])) != inf){
printf("%d",min(memo[0][0][0][0],min(memo[1][0][0][0],memo[2][0][0][0])));
}
else{
printf("-1");
}
}
/*
6
GGGGRRR
GRGRGRG
RGRGYG
*/
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:18:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf(" %d",&N);
| ~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:21:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf(" %c",&lst[i]);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
13044 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
8792 KB |
Output is correct |
14 |
Correct |
1 ms |
10844 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
14940 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
13044 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
8792 KB |
Output is correct |
14 |
Correct |
1 ms |
10844 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
14940 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
5 ms |
24412 KB |
Output is correct |
19 |
Correct |
3 ms |
20060 KB |
Output is correct |
20 |
Correct |
3 ms |
24412 KB |
Output is correct |
21 |
Correct |
4 ms |
22364 KB |
Output is correct |
22 |
Correct |
3 ms |
23900 KB |
Output is correct |
23 |
Correct |
3 ms |
20060 KB |
Output is correct |
24 |
Correct |
2 ms |
15708 KB |
Output is correct |
25 |
Correct |
9 ms |
34144 KB |
Output is correct |
26 |
Correct |
6 ms |
30044 KB |
Output is correct |
27 |
Correct |
4 ms |
27228 KB |
Output is correct |
28 |
Correct |
3 ms |
22364 KB |
Output is correct |
29 |
Correct |
4 ms |
22360 KB |
Output is correct |
30 |
Correct |
4 ms |
20316 KB |
Output is correct |
31 |
Correct |
3 ms |
18012 KB |
Output is correct |
32 |
Correct |
3 ms |
20316 KB |
Output is correct |
33 |
Correct |
4 ms |
25692 KB |
Output is correct |
34 |
Correct |
4 ms |
23132 KB |
Output is correct |
35 |
Correct |
4 ms |
24668 KB |
Output is correct |
36 |
Correct |
3 ms |
22108 KB |
Output is correct |
37 |
Correct |
3 ms |
17756 KB |
Output is correct |
38 |
Correct |
3 ms |
18044 KB |
Output is correct |
39 |
Correct |
5 ms |
20316 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
2 ms |
13916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
235 ms |
240156 KB |
Output is correct |
3 |
Correct |
208 ms |
239564 KB |
Output is correct |
4 |
Correct |
227 ms |
240080 KB |
Output is correct |
5 |
Correct |
227 ms |
240160 KB |
Output is correct |
6 |
Correct |
232 ms |
240212 KB |
Output is correct |
7 |
Correct |
229 ms |
236996 KB |
Output is correct |
8 |
Correct |
215 ms |
237084 KB |
Output is correct |
9 |
Correct |
220 ms |
236300 KB |
Output is correct |
10 |
Correct |
219 ms |
240212 KB |
Output is correct |
11 |
Correct |
219 ms |
240276 KB |
Output is correct |
12 |
Correct |
39 ms |
97184 KB |
Output is correct |
13 |
Correct |
81 ms |
139560 KB |
Output is correct |
14 |
Correct |
120 ms |
179188 KB |
Output is correct |
15 |
Correct |
216 ms |
237812 KB |
Output is correct |
16 |
Correct |
249 ms |
237768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10844 KB |
Output is correct |
7 |
Correct |
2 ms |
13044 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10844 KB |
Output is correct |
10 |
Correct |
2 ms |
14940 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
8792 KB |
Output is correct |
14 |
Correct |
1 ms |
10844 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
2 ms |
14940 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
5 ms |
24412 KB |
Output is correct |
19 |
Correct |
3 ms |
20060 KB |
Output is correct |
20 |
Correct |
3 ms |
24412 KB |
Output is correct |
21 |
Correct |
4 ms |
22364 KB |
Output is correct |
22 |
Correct |
3 ms |
23900 KB |
Output is correct |
23 |
Correct |
3 ms |
20060 KB |
Output is correct |
24 |
Correct |
2 ms |
15708 KB |
Output is correct |
25 |
Correct |
9 ms |
34144 KB |
Output is correct |
26 |
Correct |
6 ms |
30044 KB |
Output is correct |
27 |
Correct |
4 ms |
27228 KB |
Output is correct |
28 |
Correct |
3 ms |
22364 KB |
Output is correct |
29 |
Correct |
4 ms |
22360 KB |
Output is correct |
30 |
Correct |
4 ms |
20316 KB |
Output is correct |
31 |
Correct |
3 ms |
18012 KB |
Output is correct |
32 |
Correct |
3 ms |
20316 KB |
Output is correct |
33 |
Correct |
4 ms |
25692 KB |
Output is correct |
34 |
Correct |
4 ms |
23132 KB |
Output is correct |
35 |
Correct |
4 ms |
24668 KB |
Output is correct |
36 |
Correct |
3 ms |
22108 KB |
Output is correct |
37 |
Correct |
3 ms |
17756 KB |
Output is correct |
38 |
Correct |
3 ms |
18044 KB |
Output is correct |
39 |
Correct |
5 ms |
20316 KB |
Output is correct |
40 |
Correct |
1 ms |
6492 KB |
Output is correct |
41 |
Correct |
2 ms |
13916 KB |
Output is correct |
42 |
Correct |
1 ms |
8540 KB |
Output is correct |
43 |
Correct |
235 ms |
240156 KB |
Output is correct |
44 |
Correct |
208 ms |
239564 KB |
Output is correct |
45 |
Correct |
227 ms |
240080 KB |
Output is correct |
46 |
Correct |
227 ms |
240160 KB |
Output is correct |
47 |
Correct |
232 ms |
240212 KB |
Output is correct |
48 |
Correct |
229 ms |
236996 KB |
Output is correct |
49 |
Correct |
215 ms |
237084 KB |
Output is correct |
50 |
Correct |
220 ms |
236300 KB |
Output is correct |
51 |
Correct |
219 ms |
240212 KB |
Output is correct |
52 |
Correct |
219 ms |
240276 KB |
Output is correct |
53 |
Correct |
39 ms |
97184 KB |
Output is correct |
54 |
Correct |
81 ms |
139560 KB |
Output is correct |
55 |
Correct |
120 ms |
179188 KB |
Output is correct |
56 |
Correct |
216 ms |
237812 KB |
Output is correct |
57 |
Correct |
249 ms |
237768 KB |
Output is correct |
58 |
Correct |
92 ms |
126172 KB |
Output is correct |
59 |
Correct |
117 ms |
150208 KB |
Output is correct |
60 |
Correct |
108 ms |
142164 KB |
Output is correct |
61 |
Correct |
115 ms |
133460 KB |
Output is correct |
62 |
Correct |
210 ms |
242508 KB |
Output is correct |
63 |
Correct |
215 ms |
230480 KB |
Output is correct |
64 |
Correct |
210 ms |
209452 KB |
Output is correct |
65 |
Correct |
156 ms |
181844 KB |
Output is correct |
66 |
Correct |
102 ms |
136276 KB |
Output is correct |
67 |
Correct |
102 ms |
119888 KB |
Output is correct |
68 |
Correct |
106 ms |
138288 KB |
Output is correct |
69 |
Correct |
104 ms |
135752 KB |
Output is correct |
70 |
Correct |
106 ms |
139856 KB |
Output is correct |
71 |
Correct |
116 ms |
146820 KB |
Output is correct |
72 |
Correct |
84 ms |
225884 KB |
Output is correct |
73 |
Correct |
3 ms |
11612 KB |
Output is correct |