#include <bits/stdc++.h>
using namespace std;
const int N = 405, INF = int(1e9);
int n, tt[3], s[6][N], p[3][N], d[N][N][N][3];
char b[N];
bool c[N][N][N][3];
int f(int r, int g, int y, int l){
if(c[r][g][y][l]) return d[r][g][y][l];
c[r][g][y][l] = 1;
int &ret = d[r][g][y][l];
ret = INF;
if(r > tt[0] || g > tt[1] || y > tt[2]) return ret;
if(r + g + y == n) return ret = 0;
int a[5] = {r, g, y, r, g};
for(int i = 0; i < 3; i++){
if(i == l) continue;
ret = min(ret,
f(r + (i == 0), g + (i == 1), y + (i == 2), i)
+ max(0, s[i + 1][p[i][a[i]]] - a[i + 1])
+ max(0, s[i + 2][p[i][a[i]]] - a[i + 2]));
}
return ret;
}
int main(){
scanf("%d%s", &n, b + 1);
for(int i = 1; i <= n; i++){
for(int j = 0; j < 6; j++) s[j][i] = s[j][i - 1];
int t = (b[i] == 'R' ? 0 : b[i] == 'G' ? 1 : 2);
s[t][i]++;
s[t + 3][i]++;
p[t][tt[t]++] = i;
}
int r = min({f(1, 0, 0, 0) + p[0][0] - 1,
f(0, 1, 0, 1) + p[1][0] - 1,
f(0, 0, 1, 2) + p[2][0] - 1});
if(r > INF / 2) puts("-1");
else printf("%d\n", r);
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:29:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%s", &n, b + 1);
~~~~~^~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
432 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
3 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
3 ms |
432 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
432 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
3 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
3 ms |
432 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
7 ms |
3200 KB |
Output is correct |
19 |
Correct |
6 ms |
2688 KB |
Output is correct |
20 |
Correct |
6 ms |
3072 KB |
Output is correct |
21 |
Correct |
5 ms |
3200 KB |
Output is correct |
22 |
Correct |
5 ms |
2304 KB |
Output is correct |
23 |
Correct |
5 ms |
2944 KB |
Output is correct |
24 |
Correct |
5 ms |
2176 KB |
Output is correct |
25 |
Correct |
5 ms |
1152 KB |
Output is correct |
26 |
Correct |
4 ms |
1280 KB |
Output is correct |
27 |
Correct |
5 ms |
3200 KB |
Output is correct |
28 |
Correct |
7 ms |
3200 KB |
Output is correct |
29 |
Correct |
5 ms |
3072 KB |
Output is correct |
30 |
Correct |
7 ms |
3072 KB |
Output is correct |
31 |
Correct |
4 ms |
2688 KB |
Output is correct |
32 |
Correct |
5 ms |
3200 KB |
Output is correct |
33 |
Correct |
8 ms |
1536 KB |
Output is correct |
34 |
Correct |
4 ms |
1920 KB |
Output is correct |
35 |
Correct |
6 ms |
3200 KB |
Output is correct |
36 |
Correct |
4 ms |
2688 KB |
Output is correct |
37 |
Correct |
5 ms |
2432 KB |
Output is correct |
38 |
Correct |
5 ms |
2688 KB |
Output is correct |
39 |
Correct |
5 ms |
3200 KB |
Output is correct |
40 |
Correct |
3 ms |
640 KB |
Output is correct |
41 |
Correct |
3 ms |
1152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
7 ms |
4992 KB |
Output is correct |
3 |
Correct |
6 ms |
4992 KB |
Output is correct |
4 |
Correct |
7 ms |
5036 KB |
Output is correct |
5 |
Correct |
10 ms |
4992 KB |
Output is correct |
6 |
Correct |
6 ms |
4992 KB |
Output is correct |
7 |
Correct |
7 ms |
4992 KB |
Output is correct |
8 |
Correct |
7 ms |
4992 KB |
Output is correct |
9 |
Correct |
6 ms |
4992 KB |
Output is correct |
10 |
Correct |
6 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
4992 KB |
Output is correct |
12 |
Correct |
4 ms |
2788 KB |
Output is correct |
13 |
Correct |
6 ms |
3584 KB |
Output is correct |
14 |
Correct |
6 ms |
4224 KB |
Output is correct |
15 |
Correct |
7 ms |
4992 KB |
Output is correct |
16 |
Correct |
5 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
432 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
2 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
3 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
3 ms |
432 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
7 ms |
3200 KB |
Output is correct |
19 |
Correct |
6 ms |
2688 KB |
Output is correct |
20 |
Correct |
6 ms |
3072 KB |
Output is correct |
21 |
Correct |
5 ms |
3200 KB |
Output is correct |
22 |
Correct |
5 ms |
2304 KB |
Output is correct |
23 |
Correct |
5 ms |
2944 KB |
Output is correct |
24 |
Correct |
5 ms |
2176 KB |
Output is correct |
25 |
Correct |
5 ms |
1152 KB |
Output is correct |
26 |
Correct |
4 ms |
1280 KB |
Output is correct |
27 |
Correct |
5 ms |
3200 KB |
Output is correct |
28 |
Correct |
7 ms |
3200 KB |
Output is correct |
29 |
Correct |
5 ms |
3072 KB |
Output is correct |
30 |
Correct |
7 ms |
3072 KB |
Output is correct |
31 |
Correct |
4 ms |
2688 KB |
Output is correct |
32 |
Correct |
5 ms |
3200 KB |
Output is correct |
33 |
Correct |
8 ms |
1536 KB |
Output is correct |
34 |
Correct |
4 ms |
1920 KB |
Output is correct |
35 |
Correct |
6 ms |
3200 KB |
Output is correct |
36 |
Correct |
4 ms |
2688 KB |
Output is correct |
37 |
Correct |
5 ms |
2432 KB |
Output is correct |
38 |
Correct |
5 ms |
2688 KB |
Output is correct |
39 |
Correct |
5 ms |
3200 KB |
Output is correct |
40 |
Correct |
3 ms |
640 KB |
Output is correct |
41 |
Correct |
3 ms |
1152 KB |
Output is correct |
42 |
Correct |
3 ms |
384 KB |
Output is correct |
43 |
Correct |
7 ms |
4992 KB |
Output is correct |
44 |
Correct |
6 ms |
4992 KB |
Output is correct |
45 |
Correct |
7 ms |
5036 KB |
Output is correct |
46 |
Correct |
10 ms |
4992 KB |
Output is correct |
47 |
Correct |
6 ms |
4992 KB |
Output is correct |
48 |
Correct |
7 ms |
4992 KB |
Output is correct |
49 |
Correct |
7 ms |
4992 KB |
Output is correct |
50 |
Correct |
6 ms |
4992 KB |
Output is correct |
51 |
Correct |
6 ms |
5120 KB |
Output is correct |
52 |
Correct |
6 ms |
4992 KB |
Output is correct |
53 |
Correct |
4 ms |
2788 KB |
Output is correct |
54 |
Correct |
6 ms |
3584 KB |
Output is correct |
55 |
Correct |
6 ms |
4224 KB |
Output is correct |
56 |
Correct |
7 ms |
4992 KB |
Output is correct |
57 |
Correct |
5 ms |
4992 KB |
Output is correct |
58 |
Execution timed out |
520 ms |
94660 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |