#include<bits/stdc++.h>
using namespace std;
#define N 405
int dp[N][N][N][3],sz[3],qs[3][N],ans;
char s[N];
vector<int> pos[3];
int main(){
int n,i,j,k,l,idx,x;
scanf("%d %s",&n,s+1);
for(i=1;i<=n;i++){
idx=(s[i]=='R')?0:(s[i]=='G')?1:2;
pos[idx].push_back(i);
qs[idx][i]++;
sz[idx]++;
for(j=0;j<3;j++)qs[j][i]+=qs[j][i-1];
}
for(i=0;i<=sz[0];i++)for(j=0;j<=sz[1];j++)for(k=0;k<=sz[2];k++)for(l=0;l<3;l++)dp[i][j][k][l]=1e9;
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
for(i=0;i<=sz[0];i++)for(j=0;j<=sz[1];j++)for(k=0;k<=sz[2];k++)for(l=0;l<3;l++){
if(!i&&!j&&!k)continue;
if(i&&l==0){
idx=pos[0][i-1];
x=idx-min(qs[0][idx],i)-min(qs[1][idx],j)-min(qs[2][idx],k);
dp[i][j][k][l]=min(dp[i-1][j][k][1],dp[i-1][j][k][2])+x;
}
if(j&&l==1){
idx=pos[1][j-1];
x=idx-min(qs[0][idx],i)-min(qs[1][idx],j)-min(qs[2][idx],k);
dp[i][j][k][l]=min(dp[i][j-1][k][0],dp[i][j-1][k][2])+x;
}
if(k&&l==2){
idx=pos[2][k-1];
x=idx-min(qs[0][idx],i)-min(qs[1][idx],j)-min(qs[2][idx],k);
dp[i][j][k][l]=min(dp[i][j][k-1][0],dp[i][j][k-1][1])+x;
}
}
ans=min({dp[sz[0]][sz[1]][sz[2]][0],dp[sz[0]][sz[1]][sz[2]][1],dp[sz[0]][sz[1]][sz[2]][2]});
if(ans>=1e9)printf("-1");
else printf("%d",ans);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:11:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | scanf("%d %s",&n,s+1);
| ~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
440 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
440 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
2232 KB |
Output is correct |
19 |
Correct |
1 ms |
1876 KB |
Output is correct |
20 |
Correct |
1 ms |
2264 KB |
Output is correct |
21 |
Correct |
2 ms |
2360 KB |
Output is correct |
22 |
Correct |
1 ms |
1588 KB |
Output is correct |
23 |
Correct |
1 ms |
2004 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4052 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2132 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2388 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
1876 KB |
Output is correct |
37 |
Correct |
2 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2292 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
65 ms |
162952 KB |
Output is correct |
3 |
Correct |
59 ms |
162152 KB |
Output is correct |
4 |
Correct |
67 ms |
162960 KB |
Output is correct |
5 |
Correct |
71 ms |
162860 KB |
Output is correct |
6 |
Correct |
60 ms |
162912 KB |
Output is correct |
7 |
Correct |
61 ms |
162108 KB |
Output is correct |
8 |
Correct |
63 ms |
162132 KB |
Output is correct |
9 |
Correct |
60 ms |
161344 KB |
Output is correct |
10 |
Correct |
61 ms |
162896 KB |
Output is correct |
11 |
Correct |
65 ms |
162864 KB |
Output is correct |
12 |
Correct |
21 ms |
43968 KB |
Output is correct |
13 |
Correct |
36 ms |
77060 KB |
Output is correct |
14 |
Correct |
49 ms |
111436 KB |
Output is correct |
15 |
Correct |
60 ms |
162828 KB |
Output is correct |
16 |
Correct |
63 ms |
162912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
440 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
2 ms |
2232 KB |
Output is correct |
19 |
Correct |
1 ms |
1876 KB |
Output is correct |
20 |
Correct |
1 ms |
2264 KB |
Output is correct |
21 |
Correct |
2 ms |
2360 KB |
Output is correct |
22 |
Correct |
1 ms |
1588 KB |
Output is correct |
23 |
Correct |
1 ms |
2004 KB |
Output is correct |
24 |
Correct |
1 ms |
1620 KB |
Output is correct |
25 |
Correct |
2 ms |
3924 KB |
Output is correct |
26 |
Correct |
2 ms |
4052 KB |
Output is correct |
27 |
Correct |
2 ms |
3028 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2132 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
1 ms |
2004 KB |
Output is correct |
32 |
Correct |
1 ms |
2388 KB |
Output is correct |
33 |
Correct |
2 ms |
3796 KB |
Output is correct |
34 |
Correct |
2 ms |
3540 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
2 ms |
1876 KB |
Output is correct |
37 |
Correct |
2 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2292 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
65 ms |
162952 KB |
Output is correct |
44 |
Correct |
59 ms |
162152 KB |
Output is correct |
45 |
Correct |
67 ms |
162960 KB |
Output is correct |
46 |
Correct |
71 ms |
162860 KB |
Output is correct |
47 |
Correct |
60 ms |
162912 KB |
Output is correct |
48 |
Correct |
61 ms |
162108 KB |
Output is correct |
49 |
Correct |
63 ms |
162132 KB |
Output is correct |
50 |
Correct |
60 ms |
161344 KB |
Output is correct |
51 |
Correct |
61 ms |
162896 KB |
Output is correct |
52 |
Correct |
65 ms |
162864 KB |
Output is correct |
53 |
Correct |
21 ms |
43968 KB |
Output is correct |
54 |
Correct |
36 ms |
77060 KB |
Output is correct |
55 |
Correct |
49 ms |
111436 KB |
Output is correct |
56 |
Correct |
60 ms |
162828 KB |
Output is correct |
57 |
Correct |
63 ms |
162912 KB |
Output is correct |
58 |
Correct |
65 ms |
76204 KB |
Output is correct |
59 |
Correct |
74 ms |
96460 KB |
Output is correct |
60 |
Correct |
67 ms |
87812 KB |
Output is correct |
61 |
Correct |
64 ms |
84872 KB |
Output is correct |
62 |
Correct |
64 ms |
160544 KB |
Output is correct |
63 |
Correct |
66 ms |
160200 KB |
Output is correct |
64 |
Correct |
92 ms |
150224 KB |
Output is correct |
65 |
Correct |
84 ms |
138452 KB |
Output is correct |
66 |
Correct |
63 ms |
86088 KB |
Output is correct |
67 |
Correct |
64 ms |
82832 KB |
Output is correct |
68 |
Correct |
65 ms |
88664 KB |
Output is correct |
69 |
Correct |
66 ms |
85448 KB |
Output is correct |
70 |
Correct |
86 ms |
92460 KB |
Output is correct |
71 |
Correct |
68 ms |
83932 KB |
Output is correct |
72 |
Correct |
29 ms |
41568 KB |
Output is correct |
73 |
Correct |
3 ms |
2772 KB |
Output is correct |