#include <bits/stdc++.h>
using namespace std;
#define loop(_i, _a, _b) for(int _i=_a; _i<=_b; ++_i)
const int maxn=401, maxn2=201, inf=1e9;
int n;
int cnt[4][maxn][4];
int val[maxn];
string s;
int main() {
cin.tie(0) -> ios_base::sync_with_stdio(0);
cin >> n >> s;
int a=0, b=0, c=0;
loop(i, 1, n) {
int v;
if(s[i-1] == 'R') v=1;
if(s[i-1] == 'G') v=2;
if(s[i-1] == 'Y') v=3;
val[i]=v;
if(v==1) {
++a;
cnt[1][a][2]=b;
cnt[1][a][3]=c;
}
if(v==2) {
++b;
cnt[2][b][1]=a;
cnt[2][b][3]=c;
}
if(v==3) {
++c;
cnt[3][c][1]=a;
cnt[3][c][2]=b;
}
}
vector<vector<vector<vector<int>>>> dp(a+1, vector<vector<vector<int>>>(b+1, vector<vector<int>>(c+1, vector<int>(4, inf))));
dp[0][0][0][1]=0;
dp[0][0][0][2]=0;
dp[0][0][0][3]=0;
loop(i, 0, a) {
loop(j, 0, b) {
loop(k, 0, c) {
if(i<a) dp[i+1][j][k][1]=min(dp[i+1][j][k][1], min(dp[i][j][k][2], dp[i][j][k][3])+abs(j-cnt[1][i+1][2])+abs(k-cnt[1][i+1][3]));
if(j<b) dp[i][j+1][k][2]=min(dp[i][j+1][k][2], min(dp[i][j][k][1], dp[i][j][k][3])+abs(i-cnt[2][j+1][1])+abs(k-cnt[2][j+1][3]));
if(k<c) dp[i][j][k+1][3]=min(dp[i][j][k+1][3], min(dp[i][j][k][1], dp[i][j][k][2])+abs(i-cnt[3][k+1][1])+abs(j-cnt[3][k+1][2]));
}
}
}
int res = inf;
loop(i, 1, 3) res=min(res, dp[a][b][c][i]);
if(res==inf) cout << "-1";
else cout << res/2;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:25:5: warning: 'v' may be used uninitialized in this function [-Wmaybe-uninitialized]
25 | if(v==2) {
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
852 KB |
Output is correct |
20 |
Correct |
1 ms |
832 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
708 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
852 KB |
Output is correct |
31 |
Correct |
1 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
836 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
576 KB |
Output is correct |
35 |
Correct |
1 ms |
724 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
38 |
Correct |
1 ms |
728 KB |
Output is correct |
39 |
Correct |
1 ms |
852 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
4 ms |
3776 KB |
Output is correct |
3 |
Correct |
4 ms |
3796 KB |
Output is correct |
4 |
Correct |
4 ms |
3796 KB |
Output is correct |
5 |
Correct |
4 ms |
3796 KB |
Output is correct |
6 |
Correct |
4 ms |
3836 KB |
Output is correct |
7 |
Correct |
4 ms |
3796 KB |
Output is correct |
8 |
Correct |
4 ms |
3776 KB |
Output is correct |
9 |
Correct |
4 ms |
3752 KB |
Output is correct |
10 |
Correct |
4 ms |
3796 KB |
Output is correct |
11 |
Correct |
4 ms |
3796 KB |
Output is correct |
12 |
Correct |
1 ms |
1284 KB |
Output is correct |
13 |
Correct |
3 ms |
1984 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
4 ms |
3796 KB |
Output is correct |
16 |
Correct |
4 ms |
3796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
852 KB |
Output is correct |
20 |
Correct |
1 ms |
832 KB |
Output is correct |
21 |
Correct |
1 ms |
852 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
852 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
708 KB |
Output is correct |
28 |
Correct |
1 ms |
852 KB |
Output is correct |
29 |
Correct |
1 ms |
852 KB |
Output is correct |
30 |
Correct |
1 ms |
852 KB |
Output is correct |
31 |
Correct |
1 ms |
852 KB |
Output is correct |
32 |
Correct |
1 ms |
836 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
1 ms |
576 KB |
Output is correct |
35 |
Correct |
1 ms |
724 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
724 KB |
Output is correct |
38 |
Correct |
1 ms |
728 KB |
Output is correct |
39 |
Correct |
1 ms |
852 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
468 KB |
Output is correct |
42 |
Correct |
1 ms |
212 KB |
Output is correct |
43 |
Correct |
4 ms |
3776 KB |
Output is correct |
44 |
Correct |
4 ms |
3796 KB |
Output is correct |
45 |
Correct |
4 ms |
3796 KB |
Output is correct |
46 |
Correct |
4 ms |
3796 KB |
Output is correct |
47 |
Correct |
4 ms |
3836 KB |
Output is correct |
48 |
Correct |
4 ms |
3796 KB |
Output is correct |
49 |
Correct |
4 ms |
3776 KB |
Output is correct |
50 |
Correct |
4 ms |
3752 KB |
Output is correct |
51 |
Correct |
4 ms |
3796 KB |
Output is correct |
52 |
Correct |
4 ms |
3796 KB |
Output is correct |
53 |
Correct |
1 ms |
1284 KB |
Output is correct |
54 |
Correct |
3 ms |
1984 KB |
Output is correct |
55 |
Correct |
3 ms |
2644 KB |
Output is correct |
56 |
Correct |
4 ms |
3796 KB |
Output is correct |
57 |
Correct |
4 ms |
3796 KB |
Output is correct |
58 |
Correct |
145 ms |
133212 KB |
Output is correct |
59 |
Correct |
136 ms |
133280 KB |
Output is correct |
60 |
Correct |
134 ms |
132352 KB |
Output is correct |
61 |
Correct |
135 ms |
133744 KB |
Output is correct |
62 |
Correct |
13 ms |
12416 KB |
Output is correct |
63 |
Correct |
24 ms |
20764 KB |
Output is correct |
64 |
Correct |
81 ms |
66588 KB |
Output is correct |
65 |
Correct |
108 ms |
95588 KB |
Output is correct |
66 |
Correct |
133 ms |
131708 KB |
Output is correct |
67 |
Correct |
130 ms |
131748 KB |
Output is correct |
68 |
Correct |
136 ms |
133744 KB |
Output is correct |
69 |
Correct |
157 ms |
134604 KB |
Output is correct |
70 |
Correct |
134 ms |
134176 KB |
Output is correct |
71 |
Correct |
132 ms |
131036 KB |
Output is correct |
72 |
Correct |
40 ms |
34124 KB |
Output is correct |
73 |
Correct |
10 ms |
10580 KB |
Output is correct |