#include <bits/stdc++.h>
using namespace std;
const int MAXN = 405;
int memo[MAXN][MAXN][MAXN][3], pos[3][MAXN], cnt[3], pref[3][MAXN];
const int inf = 1e9 + 7;
int dp(int c0, int c1, int c2, int fr){
if (c0 == 0 && c1 == 0 && c2 == 0) return 0;
if (memo[c0][c1][c2][fr] != -1) return memo[c0][c1][c2][fr];
int ans = inf;
if (fr == 0){
if (c0 != 0){
int ipos = pos[0][c0], swps = ipos + max(c1 - pref[1][ipos], 0) + max(c2 - pref[2][ipos], 0) - (c0 + c1 + c2);
ans = min(ans, min(dp(c0 - 1, c1, c2, 1), dp(c0 - 1, c1, c2, 2)) + swps);
}
}
else if (fr == 1){
if (c1 != 0){
int ipos = pos[1][c1], swps = ipos + max(c0 - pref[0][ipos], 0) + max(c2 - pref[2][ipos], 0) - (c0 + c1 + c2);
ans = min(ans, min(dp(c0, c1 - 1, c2, 0), dp(c0, c1 - 1, c2, 2)) + swps);
}
}
else{
if (c2 != 0){
int ipos = pos[2][c2], swps = ipos + max(c0 - pref[0][ipos], 0) + max(c1 - pref[1][ipos], 0) - (c0 + c1 + c2);
ans = min(ans, min(dp(c0, c1, c2 - 1, 0), dp(c0, c1, c2 - 1, 1)) + swps);
}
}
return memo[c0][c1][c2][fr] = ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int nums; cin >> nums;
for (int i = 1; i <= nums; i++){
char ch; cin >> ch;
int v;
if (ch == 'R') v = 0;
else if (ch == 'Y') v = 1;
else v = 2;
cnt[v]++;
pos[v][cnt[v]] = i;
for (int j = 0; j < 3; j++) pref[j][i] = pref[j][i - 1];
pref[v][i]++;
}
memset(memo, -1, sizeof(memo));
int ans = inf;
for (int j = 0; j < 3; j++) ans = min(ans, dp(cnt[0], cnt[1], cnt[2], j));
cout << ((ans == inf) ? -1 : ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
301 ms |
780224 KB |
Output is correct |
2 |
Correct |
245 ms |
780208 KB |
Output is correct |
3 |
Correct |
227 ms |
780372 KB |
Output is correct |
4 |
Correct |
247 ms |
780308 KB |
Output is correct |
5 |
Correct |
214 ms |
780372 KB |
Output is correct |
6 |
Correct |
217 ms |
780288 KB |
Output is correct |
7 |
Correct |
207 ms |
780372 KB |
Output is correct |
8 |
Correct |
208 ms |
780368 KB |
Output is correct |
9 |
Correct |
248 ms |
780408 KB |
Output is correct |
10 |
Correct |
208 ms |
780372 KB |
Output is correct |
11 |
Correct |
210 ms |
780372 KB |
Output is correct |
12 |
Correct |
208 ms |
780336 KB |
Output is correct |
13 |
Correct |
204 ms |
780204 KB |
Output is correct |
14 |
Correct |
206 ms |
780360 KB |
Output is correct |
15 |
Correct |
207 ms |
780372 KB |
Output is correct |
16 |
Correct |
207 ms |
780412 KB |
Output is correct |
17 |
Correct |
208 ms |
780232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
301 ms |
780224 KB |
Output is correct |
2 |
Correct |
245 ms |
780208 KB |
Output is correct |
3 |
Correct |
227 ms |
780372 KB |
Output is correct |
4 |
Correct |
247 ms |
780308 KB |
Output is correct |
5 |
Correct |
214 ms |
780372 KB |
Output is correct |
6 |
Correct |
217 ms |
780288 KB |
Output is correct |
7 |
Correct |
207 ms |
780372 KB |
Output is correct |
8 |
Correct |
208 ms |
780368 KB |
Output is correct |
9 |
Correct |
248 ms |
780408 KB |
Output is correct |
10 |
Correct |
208 ms |
780372 KB |
Output is correct |
11 |
Correct |
210 ms |
780372 KB |
Output is correct |
12 |
Correct |
208 ms |
780336 KB |
Output is correct |
13 |
Correct |
204 ms |
780204 KB |
Output is correct |
14 |
Correct |
206 ms |
780360 KB |
Output is correct |
15 |
Correct |
207 ms |
780372 KB |
Output is correct |
16 |
Correct |
207 ms |
780412 KB |
Output is correct |
17 |
Correct |
208 ms |
780232 KB |
Output is correct |
18 |
Correct |
205 ms |
780624 KB |
Output is correct |
19 |
Correct |
207 ms |
780368 KB |
Output is correct |
20 |
Correct |
207 ms |
780228 KB |
Output is correct |
21 |
Correct |
207 ms |
780228 KB |
Output is correct |
22 |
Correct |
207 ms |
780232 KB |
Output is correct |
23 |
Correct |
209 ms |
780344 KB |
Output is correct |
24 |
Correct |
211 ms |
780352 KB |
Output is correct |
25 |
Correct |
216 ms |
780416 KB |
Output is correct |
26 |
Correct |
208 ms |
780296 KB |
Output is correct |
27 |
Correct |
210 ms |
780348 KB |
Output is correct |
28 |
Correct |
213 ms |
780460 KB |
Output is correct |
29 |
Correct |
207 ms |
780252 KB |
Output is correct |
30 |
Correct |
208 ms |
780368 KB |
Output is correct |
31 |
Correct |
215 ms |
780392 KB |
Output is correct |
32 |
Correct |
218 ms |
780364 KB |
Output is correct |
33 |
Correct |
214 ms |
780248 KB |
Output is correct |
34 |
Correct |
212 ms |
780288 KB |
Output is correct |
35 |
Correct |
214 ms |
780368 KB |
Output is correct |
36 |
Correct |
213 ms |
780372 KB |
Output is correct |
37 |
Correct |
212 ms |
780616 KB |
Output is correct |
38 |
Correct |
211 ms |
780424 KB |
Output is correct |
39 |
Correct |
207 ms |
780380 KB |
Output is correct |
40 |
Correct |
209 ms |
780436 KB |
Output is correct |
41 |
Correct |
207 ms |
780376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
233 ms |
780372 KB |
Output is correct |
2 |
Correct |
220 ms |
780368 KB |
Output is correct |
3 |
Correct |
208 ms |
780380 KB |
Output is correct |
4 |
Correct |
206 ms |
780372 KB |
Output is correct |
5 |
Correct |
207 ms |
780312 KB |
Output is correct |
6 |
Correct |
207 ms |
780368 KB |
Output is correct |
7 |
Correct |
207 ms |
780372 KB |
Output is correct |
8 |
Correct |
221 ms |
780416 KB |
Output is correct |
9 |
Correct |
207 ms |
780348 KB |
Output is correct |
10 |
Correct |
211 ms |
780476 KB |
Output is correct |
11 |
Correct |
210 ms |
780248 KB |
Output is correct |
12 |
Correct |
207 ms |
780324 KB |
Output is correct |
13 |
Correct |
211 ms |
780628 KB |
Output is correct |
14 |
Correct |
211 ms |
780368 KB |
Output is correct |
15 |
Correct |
209 ms |
780296 KB |
Output is correct |
16 |
Correct |
211 ms |
780380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
301 ms |
780224 KB |
Output is correct |
2 |
Correct |
245 ms |
780208 KB |
Output is correct |
3 |
Correct |
227 ms |
780372 KB |
Output is correct |
4 |
Correct |
247 ms |
780308 KB |
Output is correct |
5 |
Correct |
214 ms |
780372 KB |
Output is correct |
6 |
Correct |
217 ms |
780288 KB |
Output is correct |
7 |
Correct |
207 ms |
780372 KB |
Output is correct |
8 |
Correct |
208 ms |
780368 KB |
Output is correct |
9 |
Correct |
248 ms |
780408 KB |
Output is correct |
10 |
Correct |
208 ms |
780372 KB |
Output is correct |
11 |
Correct |
210 ms |
780372 KB |
Output is correct |
12 |
Correct |
208 ms |
780336 KB |
Output is correct |
13 |
Correct |
204 ms |
780204 KB |
Output is correct |
14 |
Correct |
206 ms |
780360 KB |
Output is correct |
15 |
Correct |
207 ms |
780372 KB |
Output is correct |
16 |
Correct |
207 ms |
780412 KB |
Output is correct |
17 |
Correct |
208 ms |
780232 KB |
Output is correct |
18 |
Correct |
205 ms |
780624 KB |
Output is correct |
19 |
Correct |
207 ms |
780368 KB |
Output is correct |
20 |
Correct |
207 ms |
780228 KB |
Output is correct |
21 |
Correct |
207 ms |
780228 KB |
Output is correct |
22 |
Correct |
207 ms |
780232 KB |
Output is correct |
23 |
Correct |
209 ms |
780344 KB |
Output is correct |
24 |
Correct |
211 ms |
780352 KB |
Output is correct |
25 |
Correct |
216 ms |
780416 KB |
Output is correct |
26 |
Correct |
208 ms |
780296 KB |
Output is correct |
27 |
Correct |
210 ms |
780348 KB |
Output is correct |
28 |
Correct |
213 ms |
780460 KB |
Output is correct |
29 |
Correct |
207 ms |
780252 KB |
Output is correct |
30 |
Correct |
208 ms |
780368 KB |
Output is correct |
31 |
Correct |
215 ms |
780392 KB |
Output is correct |
32 |
Correct |
218 ms |
780364 KB |
Output is correct |
33 |
Correct |
214 ms |
780248 KB |
Output is correct |
34 |
Correct |
212 ms |
780288 KB |
Output is correct |
35 |
Correct |
214 ms |
780368 KB |
Output is correct |
36 |
Correct |
213 ms |
780372 KB |
Output is correct |
37 |
Correct |
212 ms |
780616 KB |
Output is correct |
38 |
Correct |
211 ms |
780424 KB |
Output is correct |
39 |
Correct |
207 ms |
780380 KB |
Output is correct |
40 |
Correct |
209 ms |
780436 KB |
Output is correct |
41 |
Correct |
207 ms |
780376 KB |
Output is correct |
42 |
Correct |
233 ms |
780372 KB |
Output is correct |
43 |
Correct |
220 ms |
780368 KB |
Output is correct |
44 |
Correct |
208 ms |
780380 KB |
Output is correct |
45 |
Correct |
206 ms |
780372 KB |
Output is correct |
46 |
Correct |
207 ms |
780312 KB |
Output is correct |
47 |
Correct |
207 ms |
780368 KB |
Output is correct |
48 |
Correct |
207 ms |
780372 KB |
Output is correct |
49 |
Correct |
221 ms |
780416 KB |
Output is correct |
50 |
Correct |
207 ms |
780348 KB |
Output is correct |
51 |
Correct |
211 ms |
780476 KB |
Output is correct |
52 |
Correct |
210 ms |
780248 KB |
Output is correct |
53 |
Correct |
207 ms |
780324 KB |
Output is correct |
54 |
Correct |
211 ms |
780628 KB |
Output is correct |
55 |
Correct |
211 ms |
780368 KB |
Output is correct |
56 |
Correct |
209 ms |
780296 KB |
Output is correct |
57 |
Correct |
211 ms |
780380 KB |
Output is correct |
58 |
Correct |
275 ms |
780480 KB |
Output is correct |
59 |
Correct |
265 ms |
780372 KB |
Output is correct |
60 |
Correct |
271 ms |
780368 KB |
Output is correct |
61 |
Correct |
264 ms |
780368 KB |
Output is correct |
62 |
Correct |
213 ms |
780364 KB |
Output is correct |
63 |
Correct |
209 ms |
780372 KB |
Output is correct |
64 |
Correct |
218 ms |
780652 KB |
Output is correct |
65 |
Correct |
229 ms |
780368 KB |
Output is correct |
66 |
Correct |
265 ms |
780320 KB |
Output is correct |
67 |
Correct |
267 ms |
780500 KB |
Output is correct |
68 |
Correct |
296 ms |
780368 KB |
Output is correct |
69 |
Correct |
276 ms |
780484 KB |
Output is correct |
70 |
Correct |
302 ms |
780480 KB |
Output is correct |
71 |
Correct |
264 ms |
780372 KB |
Output is correct |
72 |
Correct |
210 ms |
780272 KB |
Output is correct |
73 |
Correct |
209 ms |
780216 KB |
Output is correct |