#include <bits/stdc++.h>
using namespace std;
int n;
int f[4];
int a[401];
char s[401];
int pos[4][401];
int ad[401][4];
long long dp[201][201][201][4];
int main()
{
// freopen("1.in", "r", stdin);
scanf("%d", &n);
scanf("%s", s + 1);
for(int i = 1; i <= n ; ++i){
if(s[i] == 'R') a[i] = 1;
if(s[i] == 'Y') a[i] = 2;
if(s[i] == 'G') a[i] = 3;
++f[a[i]];
pos[a[i]][f[a[i]]] = i;
}
if(f[1] > n / 2 + n % 2 || f[2] > n / 2 + n % 2 || f[3] > n / 2 + n % 2){
printf("-1");
return 0;
}
for(int i = 1; i <= 3 ; ++i){
int NR = 0;
for(int p = 1; p <= n ; ++p){
if(a[p] == i) ++NR;
ad[p][i] = NR;
}
}
memset(dp, 0x3f3f3f3f, sizeof(dp));
if(f[1] > 0) dp[1][0][0][1] = pos[1][1] - 1;
if(f[2] > 0) dp[0][1][0][2] = pos[2][1] - 1;
if(f[3] > 0) dp[0][0][1][3] = pos[3][1] - 1;
int a, b, c, a2, b2, c2, l1, l2;
for(int i = 2; i <= n ; ++i){
for(a = 0; a <= i && a <= f[1] ; ++a){
for(b = 0; b <= i - a && b <= f[2] ; ++b){
c = i - a - b;
if(c > f[3]) continue ;
for(int last = 1; last <= 3 ; ++last){
a2 = a, b2 = b, c2 = c;
if(last == 1) --a2, l1 = 2, l2 = 3;
if(last == 2) --b2, l1 = 1, l2 = 3;
if(last == 3) --c2, l1 = 1, l2 = 2;
if(a2 < 0 || b2 < 0 || c2 < 0) continue ;
if(last == 1){
int act_pos = pos[1][a] + max(0, b - ad[pos[1][a]][2]) + max(0, c - ad[pos[1][a]][3]);
dp[a][b][c][last] = min(dp[a][b][c][last], min(dp[a2][b2][c2][l1], dp[a2][b2][c2][l2]) + abs(act_pos - i));
}
else if(last == 2){
int act_pos = pos[2][b] + max(0, a - ad[pos[2][b]][1]) + max(0, c - ad[pos[2][b]][3]);
dp[a][b][c][last] = min(dp[a][b][c][last], min(dp[a2][b2][c2][l1], dp[a2][b2][c2][l2]) + abs(act_pos - i));
}
else if(last == 3){
int act_pos = pos[3][c] + max(0, a - ad[pos[3][c]][1]) + max(0, b - ad[pos[3][c]][2]);
dp[a][b][c][last] = min(dp[a][b][c][last], min(dp[a2][b2][c2][l1], dp[a2][b2][c2][l2]) + abs(act_pos - i));
}
}
}
}
}
long long Sol = dp[f[1]][f[2]][f[3]][1];
for(int i = 2; i <= n ; ++i) Sol = min(Sol, dp[f[1]][f[2]][f[3]][i]);
printf("%lld", Sol);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:16:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", s + 1);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
219 ms |
254644 KB |
Output is correct |
2 |
Correct |
256 ms |
254740 KB |
Output is correct |
3 |
Correct |
221 ms |
254712 KB |
Output is correct |
4 |
Correct |
230 ms |
254584 KB |
Output is correct |
5 |
Correct |
221 ms |
254648 KB |
Output is correct |
6 |
Correct |
219 ms |
254584 KB |
Output is correct |
7 |
Correct |
219 ms |
254584 KB |
Output is correct |
8 |
Correct |
226 ms |
254712 KB |
Output is correct |
9 |
Correct |
223 ms |
254584 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
226 ms |
254616 KB |
Output is correct |
12 |
Correct |
217 ms |
254584 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
227 ms |
254712 KB |
Output is correct |
15 |
Correct |
223 ms |
254588 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
219 ms |
254644 KB |
Output is correct |
2 |
Correct |
256 ms |
254740 KB |
Output is correct |
3 |
Correct |
221 ms |
254712 KB |
Output is correct |
4 |
Correct |
230 ms |
254584 KB |
Output is correct |
5 |
Correct |
221 ms |
254648 KB |
Output is correct |
6 |
Correct |
219 ms |
254584 KB |
Output is correct |
7 |
Correct |
219 ms |
254584 KB |
Output is correct |
8 |
Correct |
226 ms |
254712 KB |
Output is correct |
9 |
Correct |
223 ms |
254584 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
226 ms |
254616 KB |
Output is correct |
12 |
Correct |
217 ms |
254584 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
227 ms |
254712 KB |
Output is correct |
15 |
Correct |
223 ms |
254588 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
217 ms |
254644 KB |
Output is correct |
19 |
Correct |
224 ms |
254584 KB |
Output is correct |
20 |
Correct |
219 ms |
254584 KB |
Output is correct |
21 |
Correct |
220 ms |
254620 KB |
Output is correct |
22 |
Correct |
221 ms |
254584 KB |
Output is correct |
23 |
Correct |
222 ms |
254584 KB |
Output is correct |
24 |
Correct |
230 ms |
254624 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
221 ms |
254736 KB |
Output is correct |
28 |
Correct |
217 ms |
254584 KB |
Output is correct |
29 |
Correct |
219 ms |
254536 KB |
Output is correct |
30 |
Correct |
222 ms |
254524 KB |
Output is correct |
31 |
Correct |
217 ms |
254584 KB |
Output is correct |
32 |
Correct |
224 ms |
254584 KB |
Output is correct |
33 |
Correct |
2 ms |
256 KB |
Output is correct |
34 |
Correct |
2 ms |
256 KB |
Output is correct |
35 |
Correct |
282 ms |
254656 KB |
Output is correct |
36 |
Correct |
219 ms |
254584 KB |
Output is correct |
37 |
Correct |
219 ms |
254584 KB |
Output is correct |
38 |
Correct |
219 ms |
254584 KB |
Output is correct |
39 |
Correct |
223 ms |
254636 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
220 ms |
254584 KB |
Output is correct |
2 |
Correct |
221 ms |
254584 KB |
Output is correct |
3 |
Correct |
220 ms |
254560 KB |
Output is correct |
4 |
Correct |
220 ms |
254556 KB |
Output is correct |
5 |
Correct |
220 ms |
254800 KB |
Output is correct |
6 |
Correct |
220 ms |
254584 KB |
Output is correct |
7 |
Correct |
219 ms |
254588 KB |
Output is correct |
8 |
Correct |
230 ms |
254652 KB |
Output is correct |
9 |
Correct |
219 ms |
254544 KB |
Output is correct |
10 |
Correct |
219 ms |
254584 KB |
Output is correct |
11 |
Correct |
221 ms |
254584 KB |
Output is correct |
12 |
Correct |
224 ms |
254712 KB |
Output is correct |
13 |
Correct |
221 ms |
254584 KB |
Output is correct |
14 |
Correct |
224 ms |
254604 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
219 ms |
254644 KB |
Output is correct |
2 |
Correct |
256 ms |
254740 KB |
Output is correct |
3 |
Correct |
221 ms |
254712 KB |
Output is correct |
4 |
Correct |
230 ms |
254584 KB |
Output is correct |
5 |
Correct |
221 ms |
254648 KB |
Output is correct |
6 |
Correct |
219 ms |
254584 KB |
Output is correct |
7 |
Correct |
219 ms |
254584 KB |
Output is correct |
8 |
Correct |
226 ms |
254712 KB |
Output is correct |
9 |
Correct |
223 ms |
254584 KB |
Output is correct |
10 |
Correct |
2 ms |
380 KB |
Output is correct |
11 |
Correct |
226 ms |
254616 KB |
Output is correct |
12 |
Correct |
217 ms |
254584 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
227 ms |
254712 KB |
Output is correct |
15 |
Correct |
223 ms |
254588 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
217 ms |
254644 KB |
Output is correct |
19 |
Correct |
224 ms |
254584 KB |
Output is correct |
20 |
Correct |
219 ms |
254584 KB |
Output is correct |
21 |
Correct |
220 ms |
254620 KB |
Output is correct |
22 |
Correct |
221 ms |
254584 KB |
Output is correct |
23 |
Correct |
222 ms |
254584 KB |
Output is correct |
24 |
Correct |
230 ms |
254624 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
221 ms |
254736 KB |
Output is correct |
28 |
Correct |
217 ms |
254584 KB |
Output is correct |
29 |
Correct |
219 ms |
254536 KB |
Output is correct |
30 |
Correct |
222 ms |
254524 KB |
Output is correct |
31 |
Correct |
217 ms |
254584 KB |
Output is correct |
32 |
Correct |
224 ms |
254584 KB |
Output is correct |
33 |
Correct |
2 ms |
256 KB |
Output is correct |
34 |
Correct |
2 ms |
256 KB |
Output is correct |
35 |
Correct |
282 ms |
254656 KB |
Output is correct |
36 |
Correct |
219 ms |
254584 KB |
Output is correct |
37 |
Correct |
219 ms |
254584 KB |
Output is correct |
38 |
Correct |
219 ms |
254584 KB |
Output is correct |
39 |
Correct |
223 ms |
254636 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
2 ms |
376 KB |
Output is correct |
42 |
Correct |
220 ms |
254584 KB |
Output is correct |
43 |
Correct |
221 ms |
254584 KB |
Output is correct |
44 |
Correct |
220 ms |
254560 KB |
Output is correct |
45 |
Correct |
220 ms |
254556 KB |
Output is correct |
46 |
Correct |
220 ms |
254800 KB |
Output is correct |
47 |
Correct |
220 ms |
254584 KB |
Output is correct |
48 |
Correct |
219 ms |
254588 KB |
Output is correct |
49 |
Correct |
230 ms |
254652 KB |
Output is correct |
50 |
Correct |
219 ms |
254544 KB |
Output is correct |
51 |
Correct |
219 ms |
254584 KB |
Output is correct |
52 |
Correct |
221 ms |
254584 KB |
Output is correct |
53 |
Correct |
224 ms |
254712 KB |
Output is correct |
54 |
Correct |
221 ms |
254584 KB |
Output is correct |
55 |
Correct |
224 ms |
254604 KB |
Output is correct |
56 |
Correct |
2 ms |
376 KB |
Output is correct |
57 |
Correct |
2 ms |
376 KB |
Output is correct |
58 |
Correct |
343 ms |
254748 KB |
Output is correct |
59 |
Correct |
342 ms |
254556 KB |
Output is correct |
60 |
Correct |
354 ms |
254840 KB |
Output is correct |
61 |
Correct |
357 ms |
254584 KB |
Output is correct |
62 |
Correct |
2 ms |
376 KB |
Output is correct |
63 |
Correct |
232 ms |
254584 KB |
Output is correct |
64 |
Correct |
275 ms |
254584 KB |
Output is correct |
65 |
Correct |
302 ms |
254584 KB |
Output is correct |
66 |
Correct |
342 ms |
254712 KB |
Output is correct |
67 |
Correct |
340 ms |
254780 KB |
Output is correct |
68 |
Correct |
344 ms |
254712 KB |
Output is correct |
69 |
Correct |
341 ms |
254584 KB |
Output is correct |
70 |
Correct |
344 ms |
254584 KB |
Output is correct |
71 |
Correct |
344 ms |
254728 KB |
Output is correct |
72 |
Correct |
2 ms |
376 KB |
Output is correct |
73 |
Correct |
2 ms |
376 KB |
Output is correct |