#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int inf = 1e9;
int n;
int a[410];
int cnt[410];
int inv[3][410][3];
int dp[410][410][410][3];
int main() {
int i, j, k, r, g, y;
scanf("%d", &n);
for (i=1; i<=n; i++) {
char c;
scanf(" %c", &c);
if (c == 'G') a[i] = 1;
else if (c == 'Y') a[i] = 2;
}
for (i=1; i<=n; i++) {
cnt[a[i]] ++;
for (j=0; j<3; j++) {
if (j == a[i]) continue;
inv[a[i]][cnt[a[i]]][j] = cnt[j];
}
}
int R = cnt[0], G = cnt[1], Y = cnt[2];
if (max({R, G, Y}) > (n+1)/2) {
printf("-1\n");
return 0;
}
for (i=0; i<=n; i++) for (r=0; r<=R; r++) for (g=0; g<=G; g++) for (k=0; k<3; k++) dp[i][r][g][k] = inf;
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (i=0; i<n; i++) {
for (r=0; r<=min(R, i); r++) {
for (g=0; g<=min(G, i-r); g++) {
y = i - r - g;
if (y > Y) continue;
for (k=0; k<3; k++) {
if (r < R && k != 0)
dp[i+1][r+1][g][0] = min(dp[i+1][r+1][g][0], dp[i][r][g][k] + abs(inv[0][r+1][1]-g) + abs(inv[0][r+1][2]-y));
if (g < G && k != 1)
dp[i+1][r][g+1][1] = min(dp[i+1][r][g+1][1], dp[i][r][g][k] + abs(inv[1][g+1][0]-r) + abs(inv[1][g+1][2]-y));
if (y < Y && k != 2)
dp[i+1][r][g][2] = min(dp[i+1][r][g][2], dp[i][r][g][k] + abs(inv[2][y+1][0]-r) + abs(inv[2][y+1][1]-g));
}
}
}
}
printf("%d\n", min({dp[n][R][G][0], dp[n][R][G][1], dp[n][R][G][2]}) / 2);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:18:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:21:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | scanf(" %c", &c);
| ~~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
824 KB |
Output is correct |
9 |
Correct |
1 ms |
824 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
696 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
824 KB |
Output is correct |
9 |
Correct |
1 ms |
824 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
696 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
316 KB |
Output is correct |
18 |
Correct |
3 ms |
6708 KB |
Output is correct |
19 |
Correct |
4 ms |
5588 KB |
Output is correct |
20 |
Correct |
5 ms |
7220 KB |
Output is correct |
21 |
Correct |
3 ms |
5972 KB |
Output is correct |
22 |
Correct |
3 ms |
6840 KB |
Output is correct |
23 |
Correct |
2 ms |
5204 KB |
Output is correct |
24 |
Correct |
2 ms |
3388 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
4 ms |
7864 KB |
Output is correct |
28 |
Correct |
3 ms |
6228 KB |
Output is correct |
29 |
Correct |
3 ms |
6484 KB |
Output is correct |
30 |
Correct |
2 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
4692 KB |
Output is correct |
32 |
Correct |
3 ms |
5460 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
3 ms |
6612 KB |
Output is correct |
36 |
Correct |
3 ms |
5816 KB |
Output is correct |
37 |
Correct |
2 ms |
3924 KB |
Output is correct |
38 |
Correct |
2 ms |
4436 KB |
Output is correct |
39 |
Correct |
3 ms |
5688 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
160 ms |
389684 KB |
Output is correct |
3 |
Correct |
185 ms |
388724 KB |
Output is correct |
4 |
Correct |
168 ms |
389616 KB |
Output is correct |
5 |
Correct |
167 ms |
389620 KB |
Output is correct |
6 |
Correct |
184 ms |
389656 KB |
Output is correct |
7 |
Correct |
158 ms |
386764 KB |
Output is correct |
8 |
Correct |
156 ms |
386788 KB |
Output is correct |
9 |
Correct |
160 ms |
385808 KB |
Output is correct |
10 |
Correct |
167 ms |
389668 KB |
Output is correct |
11 |
Correct |
172 ms |
389700 KB |
Output is correct |
12 |
Correct |
46 ms |
104544 KB |
Output is correct |
13 |
Correct |
74 ms |
183948 KB |
Output is correct |
14 |
Correct |
108 ms |
265920 KB |
Output is correct |
15 |
Correct |
1 ms |
312 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
716 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
1 ms |
824 KB |
Output is correct |
9 |
Correct |
1 ms |
824 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
724 KB |
Output is correct |
12 |
Correct |
1 ms |
696 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
316 KB |
Output is correct |
18 |
Correct |
3 ms |
6708 KB |
Output is correct |
19 |
Correct |
4 ms |
5588 KB |
Output is correct |
20 |
Correct |
5 ms |
7220 KB |
Output is correct |
21 |
Correct |
3 ms |
5972 KB |
Output is correct |
22 |
Correct |
3 ms |
6840 KB |
Output is correct |
23 |
Correct |
2 ms |
5204 KB |
Output is correct |
24 |
Correct |
2 ms |
3388 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
212 KB |
Output is correct |
27 |
Correct |
4 ms |
7864 KB |
Output is correct |
28 |
Correct |
3 ms |
6228 KB |
Output is correct |
29 |
Correct |
3 ms |
6484 KB |
Output is correct |
30 |
Correct |
2 ms |
5204 KB |
Output is correct |
31 |
Correct |
3 ms |
4692 KB |
Output is correct |
32 |
Correct |
3 ms |
5460 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
3 ms |
6612 KB |
Output is correct |
36 |
Correct |
3 ms |
5816 KB |
Output is correct |
37 |
Correct |
2 ms |
3924 KB |
Output is correct |
38 |
Correct |
2 ms |
4436 KB |
Output is correct |
39 |
Correct |
3 ms |
5688 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
1 ms |
212 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
160 ms |
389684 KB |
Output is correct |
44 |
Correct |
185 ms |
388724 KB |
Output is correct |
45 |
Correct |
168 ms |
389616 KB |
Output is correct |
46 |
Correct |
167 ms |
389620 KB |
Output is correct |
47 |
Correct |
184 ms |
389656 KB |
Output is correct |
48 |
Correct |
158 ms |
386764 KB |
Output is correct |
49 |
Correct |
156 ms |
386788 KB |
Output is correct |
50 |
Correct |
160 ms |
385808 KB |
Output is correct |
51 |
Correct |
167 ms |
389668 KB |
Output is correct |
52 |
Correct |
172 ms |
389700 KB |
Output is correct |
53 |
Correct |
46 ms |
104544 KB |
Output is correct |
54 |
Correct |
74 ms |
183948 KB |
Output is correct |
55 |
Correct |
108 ms |
265920 KB |
Output is correct |
56 |
Correct |
1 ms |
312 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
136 ms |
241020 KB |
Output is correct |
59 |
Correct |
153 ms |
281428 KB |
Output is correct |
60 |
Correct |
156 ms |
276160 KB |
Output is correct |
61 |
Correct |
173 ms |
259560 KB |
Output is correct |
62 |
Correct |
1 ms |
312 KB |
Output is correct |
63 |
Correct |
163 ms |
380032 KB |
Output is correct |
64 |
Correct |
169 ms |
364460 KB |
Output is correct |
65 |
Correct |
172 ms |
309836 KB |
Output is correct |
66 |
Correct |
136 ms |
264220 KB |
Output is correct |
67 |
Correct |
121 ms |
219944 KB |
Output is correct |
68 |
Correct |
146 ms |
265420 KB |
Output is correct |
69 |
Correct |
146 ms |
266140 KB |
Output is correct |
70 |
Correct |
142 ms |
264204 KB |
Output is correct |
71 |
Correct |
153 ms |
294144 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |