#include <iostream>
using namespace std;
int n,dp[401][401][401][3],cnt[3][401][3],r,g,y;
string s;
int main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> n >> s;
for (int i=0;i<n;i++){
if (s[i]=='R'){
cnt[0][++r][1]=g;
cnt[0][r][2]=y;
}
if (s[i]=='G'){
cnt[1][++g][0]=r;
cnt[1][g][2]=y;
}
if (s[i]=='Y'){
cnt[2][++y][1]=g;
cnt[2][y][0]=r;
}
}
for (int i=0;i<=r;i++)
for (int j=0;j<=g;j++)
for (int k=0;k<=y;k++)
for (int l=0;l<3;l++)
if (i+j+k)
dp[i][j][k][l]=1000000000;
for (int i=0;i<=r;i++)
for (int j=0;j<=g;j++)
for (int k=0;k<=y;k++){
if (i<r)
dp[i+1][j][k][0]=min(dp[i+1][j][k][0],min(dp[i][j][k][1],dp[i][j][k][2])+abs(cnt[0][i+1][1]-j)+abs(cnt[0][i+1][2]-k));
if (j<g)
dp[i][j+1][k][1]=min(dp[i][j+1][k][1],min(dp[i][j][k][0],dp[i][j][k][2])+abs(cnt[1][j+1][0]-i)+abs(cnt[1][j+1][2]-k));
if (k<y)
dp[i][j][k+1][2]=min(dp[i][j][k+1][2],min(dp[i][j][k][1],dp[i][j][k][0])+abs(cnt[2][k+1][1]-j)+abs(cnt[2][k+1][0]-i));
}
int res=min(min(dp[r][g][y][0],dp[r][g][y][1]),dp[r][g][y][2]);
cout << (res==1000000000?-1:res>>1);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
1876 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 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 |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3048 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
2 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 |
1 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2260 KB |
Output is correct |
40 |
Correct |
0 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
2132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
57 ms |
162948 KB |
Output is correct |
3 |
Correct |
57 ms |
162124 KB |
Output is correct |
4 |
Correct |
61 ms |
162876 KB |
Output is correct |
5 |
Correct |
59 ms |
162872 KB |
Output is correct |
6 |
Correct |
55 ms |
162940 KB |
Output is correct |
7 |
Correct |
55 ms |
162108 KB |
Output is correct |
8 |
Correct |
55 ms |
162112 KB |
Output is correct |
9 |
Correct |
57 ms |
161268 KB |
Output is correct |
10 |
Correct |
56 ms |
162884 KB |
Output is correct |
11 |
Correct |
55 ms |
162888 KB |
Output is correct |
12 |
Correct |
17 ms |
43988 KB |
Output is correct |
13 |
Correct |
28 ms |
77032 KB |
Output is correct |
14 |
Correct |
42 ms |
111248 KB |
Output is correct |
15 |
Correct |
57 ms |
162936 KB |
Output is correct |
16 |
Correct |
57 ms |
162896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
0 ms |
468 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
468 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
2260 KB |
Output is correct |
19 |
Correct |
2 ms |
1876 KB |
Output is correct |
20 |
Correct |
1 ms |
2260 KB |
Output is correct |
21 |
Correct |
1 ms |
2388 KB |
Output is correct |
22 |
Correct |
1 ms |
1620 KB |
Output is correct |
23 |
Correct |
2 ms |
2132 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 |
4180 KB |
Output is correct |
27 |
Correct |
2 ms |
3048 KB |
Output is correct |
28 |
Correct |
1 ms |
2260 KB |
Output is correct |
29 |
Correct |
1 ms |
2260 KB |
Output is correct |
30 |
Correct |
1 ms |
2260 KB |
Output is correct |
31 |
Correct |
2 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 |
1 ms |
3540 KB |
Output is correct |
35 |
Correct |
1 ms |
2644 KB |
Output is correct |
36 |
Correct |
1 ms |
1876 KB |
Output is correct |
37 |
Correct |
1 ms |
1748 KB |
Output is correct |
38 |
Correct |
1 ms |
2132 KB |
Output is correct |
39 |
Correct |
1 ms |
2260 KB |
Output is correct |
40 |
Correct |
0 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 |
57 ms |
162948 KB |
Output is correct |
44 |
Correct |
57 ms |
162124 KB |
Output is correct |
45 |
Correct |
61 ms |
162876 KB |
Output is correct |
46 |
Correct |
59 ms |
162872 KB |
Output is correct |
47 |
Correct |
55 ms |
162940 KB |
Output is correct |
48 |
Correct |
55 ms |
162108 KB |
Output is correct |
49 |
Correct |
55 ms |
162112 KB |
Output is correct |
50 |
Correct |
57 ms |
161268 KB |
Output is correct |
51 |
Correct |
56 ms |
162884 KB |
Output is correct |
52 |
Correct |
55 ms |
162888 KB |
Output is correct |
53 |
Correct |
17 ms |
43988 KB |
Output is correct |
54 |
Correct |
28 ms |
77032 KB |
Output is correct |
55 |
Correct |
42 ms |
111248 KB |
Output is correct |
56 |
Correct |
57 ms |
162936 KB |
Output is correct |
57 |
Correct |
57 ms |
162896 KB |
Output is correct |
58 |
Correct |
49 ms |
75468 KB |
Output is correct |
59 |
Correct |
58 ms |
95696 KB |
Output is correct |
60 |
Correct |
55 ms |
86904 KB |
Output is correct |
61 |
Correct |
52 ms |
84040 KB |
Output is correct |
62 |
Correct |
66 ms |
160580 KB |
Output is correct |
63 |
Correct |
61 ms |
160176 KB |
Output is correct |
64 |
Correct |
68 ms |
150184 KB |
Output is correct |
65 |
Correct |
66 ms |
138444 KB |
Output is correct |
66 |
Correct |
54 ms |
85288 KB |
Output is correct |
67 |
Correct |
53 ms |
82080 KB |
Output is correct |
68 |
Correct |
55 ms |
87868 KB |
Output is correct |
69 |
Correct |
53 ms |
84556 KB |
Output is correct |
70 |
Correct |
56 ms |
91560 KB |
Output is correct |
71 |
Correct |
59 ms |
83140 KB |
Output is correct |
72 |
Correct |
22 ms |
41172 KB |
Output is correct |
73 |
Correct |
3 ms |
2772 KB |
Output is correct |