#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pii;
const int INF = 1<<20;
int n;
char ST[410];
int P[3][410], S[3][410], F[3];
int D[2][410][410][3];
inline int _min(int x, int y){ return x<y ? x : y; }
inline int _max(int x, int y){ return x>y ? x : y; }
int dist(int r, int g, int y, int x){
int res = 0, C[3] = {r,g,y};
for(int k=0; k<3; k++)
res += _max(0, S[k][x]-C[k]);
return res;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin>>n>>(ST+1);
{
int T[128]={};
T['R'] = 0, T['G'] = 1, T['Y'] = 2;
for(int i=1; i<=n+1; i++) for(int k=0; k<3; k++)
P[k][i] = INF;
for(int i=1; i<=n; i++){
int x = T[int(ST[i])];
P[x][++F[x]] = i;
for(int k=0; k<3; k++) S[k][i] = S[k][i-1];
S[x][i]++;
}
}
for(int r=0; r<=n; r++) for(int g=0; g<=n; g++)
for(int c=0; c<3; c++) D[0][r][g][c] = INF;
D[0][0][0][0] = D[0][0][0][1] = D[0][0][0][2] = 0;
for(int i=1; i<=n; i++){
int z = i&1;
for(int r=0; r<=F[0]; r++) for(int g=0; g<=F[1]; g++){
int U[3] = {r, g, i-r-g};
for(int c=0; c<3; c++){
if(U[c]<1 || U[2]>F[2]){ D[z][r][g][c] = INF; continue; }
U[c]--;
int cost = dist(U[0],U[1],U[2],P[c][U[c]+1]-1);
int get = _min(D[!z][U[0]][U[1]][(c+1)%3], D[!z][U[0]][U[1]][(c+2)%3]);
D[z][r][g][c] = _min(INF, cost + get);
U[c]++;
}
}
// for(int r=0; r<=F[0]; r++, cout<<'\n') for(int g=0; g<=F[1]; g++, cout<<'\n'){
// for(int c=0; c<3; c++) cout<<D[z][r][g][c]<<' ';
// }
// cout<<'\n';
}
int ans = INF;
for(int r=0; r<=F[0]; r++) for(int g=0; g<=F[1]; g++)
for(int c=0; c<3; c++) ans = _min(ans, D[n&1][r][g][c]);
if(ans>=INF) ans = -1;
cout<<ans<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
768 KB |
Output is correct |
19 |
Correct |
3 ms |
768 KB |
Output is correct |
20 |
Correct |
3 ms |
768 KB |
Output is correct |
21 |
Correct |
3 ms |
768 KB |
Output is correct |
22 |
Correct |
3 ms |
768 KB |
Output is correct |
23 |
Correct |
4 ms |
768 KB |
Output is correct |
24 |
Correct |
3 ms |
768 KB |
Output is correct |
25 |
Correct |
3 ms |
768 KB |
Output is correct |
26 |
Correct |
4 ms |
768 KB |
Output is correct |
27 |
Correct |
2 ms |
768 KB |
Output is correct |
28 |
Correct |
4 ms |
768 KB |
Output is correct |
29 |
Correct |
3 ms |
768 KB |
Output is correct |
30 |
Correct |
3 ms |
768 KB |
Output is correct |
31 |
Correct |
3 ms |
640 KB |
Output is correct |
32 |
Correct |
4 ms |
816 KB |
Output is correct |
33 |
Correct |
3 ms |
768 KB |
Output is correct |
34 |
Correct |
3 ms |
768 KB |
Output is correct |
35 |
Correct |
4 ms |
768 KB |
Output is correct |
36 |
Correct |
3 ms |
768 KB |
Output is correct |
37 |
Correct |
5 ms |
640 KB |
Output is correct |
38 |
Correct |
0 ms |
640 KB |
Output is correct |
39 |
Correct |
3 ms |
768 KB |
Output is correct |
40 |
Correct |
3 ms |
640 KB |
Output is correct |
41 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
216 ms |
3292 KB |
Output is correct |
3 |
Correct |
213 ms |
3320 KB |
Output is correct |
4 |
Correct |
230 ms |
3200 KB |
Output is correct |
5 |
Correct |
176 ms |
3376 KB |
Output is correct |
6 |
Correct |
219 ms |
3320 KB |
Output is correct |
7 |
Correct |
261 ms |
3276 KB |
Output is correct |
8 |
Correct |
178 ms |
3320 KB |
Output is correct |
9 |
Correct |
240 ms |
3272 KB |
Output is correct |
10 |
Correct |
181 ms |
3328 KB |
Output is correct |
11 |
Correct |
208 ms |
3328 KB |
Output is correct |
12 |
Correct |
28 ms |
1792 KB |
Output is correct |
13 |
Correct |
65 ms |
2304 KB |
Output is correct |
14 |
Correct |
105 ms |
2816 KB |
Output is correct |
15 |
Correct |
178 ms |
3200 KB |
Output is correct |
16 |
Correct |
218 ms |
3200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
768 KB |
Output is correct |
19 |
Correct |
3 ms |
768 KB |
Output is correct |
20 |
Correct |
3 ms |
768 KB |
Output is correct |
21 |
Correct |
3 ms |
768 KB |
Output is correct |
22 |
Correct |
3 ms |
768 KB |
Output is correct |
23 |
Correct |
4 ms |
768 KB |
Output is correct |
24 |
Correct |
3 ms |
768 KB |
Output is correct |
25 |
Correct |
3 ms |
768 KB |
Output is correct |
26 |
Correct |
4 ms |
768 KB |
Output is correct |
27 |
Correct |
2 ms |
768 KB |
Output is correct |
28 |
Correct |
4 ms |
768 KB |
Output is correct |
29 |
Correct |
3 ms |
768 KB |
Output is correct |
30 |
Correct |
3 ms |
768 KB |
Output is correct |
31 |
Correct |
3 ms |
640 KB |
Output is correct |
32 |
Correct |
4 ms |
816 KB |
Output is correct |
33 |
Correct |
3 ms |
768 KB |
Output is correct |
34 |
Correct |
3 ms |
768 KB |
Output is correct |
35 |
Correct |
4 ms |
768 KB |
Output is correct |
36 |
Correct |
3 ms |
768 KB |
Output is correct |
37 |
Correct |
5 ms |
640 KB |
Output is correct |
38 |
Correct |
0 ms |
640 KB |
Output is correct |
39 |
Correct |
3 ms |
768 KB |
Output is correct |
40 |
Correct |
3 ms |
640 KB |
Output is correct |
41 |
Correct |
3 ms |
640 KB |
Output is correct |
42 |
Correct |
3 ms |
384 KB |
Output is correct |
43 |
Correct |
216 ms |
3292 KB |
Output is correct |
44 |
Correct |
213 ms |
3320 KB |
Output is correct |
45 |
Correct |
230 ms |
3200 KB |
Output is correct |
46 |
Correct |
176 ms |
3376 KB |
Output is correct |
47 |
Correct |
219 ms |
3320 KB |
Output is correct |
48 |
Correct |
261 ms |
3276 KB |
Output is correct |
49 |
Correct |
178 ms |
3320 KB |
Output is correct |
50 |
Correct |
240 ms |
3272 KB |
Output is correct |
51 |
Correct |
181 ms |
3328 KB |
Output is correct |
52 |
Correct |
208 ms |
3328 KB |
Output is correct |
53 |
Correct |
28 ms |
1792 KB |
Output is correct |
54 |
Correct |
65 ms |
2304 KB |
Output is correct |
55 |
Correct |
105 ms |
2816 KB |
Output is correct |
56 |
Correct |
178 ms |
3200 KB |
Output is correct |
57 |
Correct |
218 ms |
3200 KB |
Output is correct |
58 |
Correct |
113 ms |
2944 KB |
Output is correct |
59 |
Correct |
139 ms |
2992 KB |
Output is correct |
60 |
Correct |
147 ms |
2964 KB |
Output is correct |
61 |
Correct |
129 ms |
2944 KB |
Output is correct |
62 |
Correct |
217 ms |
3328 KB |
Output is correct |
63 |
Correct |
246 ms |
3200 KB |
Output is correct |
64 |
Correct |
199 ms |
3228 KB |
Output is correct |
65 |
Correct |
187 ms |
3072 KB |
Output is correct |
66 |
Correct |
118 ms |
2944 KB |
Output is correct |
67 |
Correct |
120 ms |
2912 KB |
Output is correct |
68 |
Correct |
127 ms |
2996 KB |
Output is correct |
69 |
Correct |
116 ms |
3020 KB |
Output is correct |
70 |
Correct |
125 ms |
3016 KB |
Output is correct |
71 |
Correct |
130 ms |
3072 KB |
Output is correct |
72 |
Correct |
59 ms |
3644 KB |
Output is correct |
73 |
Correct |
11 ms |
2304 KB |
Output is correct |