#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL * 1e18;
char S[100009], C[4] = "RGY";
vector<int> T[3];
int P[3][100009];
long long D[409][409][409][3];
inline int its(int cn, int cc, int id) {
return max(0, cc - P[cn][id]);
}
long long go(int r, int g, int y, int x) {
if(r+g+y == 0) return 0;
long long &ret = D[r][g][y][x];
if(ret != -1) return ret;
ret = INF;
if(r && x == 0) ret = min(ret, min(go(r-1, g, y, 1), go(r-1, g, y, 2)) + its(1, g, T[0][r-1]) + its(2, y, T[0][r-1]));
if(g && x == 1) ret = min(ret, min(go(r, g-1, y, 0), go(r, g-1, y, 2)) + its(0, r, T[1][g-1]) + its(2, y, T[1][g-1]));
if(y && x == 2) ret = min(ret, min(go(r, g, y-1, 0), go(r, g, y-1, 1)) + its(0, r, T[2][y-1]) + its(1, g, T[2][y-1]));
return ret;
}
int main() {
int N; scanf("%d",&N);
for(int i=1; i<=N; i++) {
scanf(" %c",&S[i]);
for(int j=0; j<3; j++) if(S[i] == C[j]) T[j].push_back(i);
}
for(int i=1; i<=N; i++) for(int j=0; j<3; j++) P[j][i] = P[j][i-1] + (S[i] == C[j]);
for(int i=0; i<=P[0][N]; i++) for(int j=0; j<=P[1][N]; j++) for(int k=0; k<=P[2][N]; k++) for(int l=0; l<3; l++) D[i][j][k][l] = -1;
long long ans = INF;
for(int i=0; i<3; i++) if(P[i][N]) ans = min(ans, go(P[0][N], P[1][N], P[2][N], i));
if(ans == INF) puts("-1");
else printf("%lld", ans);
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:26:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int N; scanf("%d",&N);
~~~~~^~~~~~~~~
joi2019_ho_t3.cpp:28:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c",&S[i]);
~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
556 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
556 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
2432 KB |
Output is correct |
19 |
Correct |
3 ms |
2048 KB |
Output is correct |
20 |
Correct |
3 ms |
2432 KB |
Output is correct |
21 |
Correct |
4 ms |
2560 KB |
Output is correct |
22 |
Correct |
3 ms |
1792 KB |
Output is correct |
23 |
Correct |
4 ms |
2304 KB |
Output is correct |
24 |
Correct |
3 ms |
1792 KB |
Output is correct |
25 |
Correct |
5 ms |
3968 KB |
Output is correct |
26 |
Correct |
4 ms |
4224 KB |
Output is correct |
27 |
Correct |
5 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
2432 KB |
Output is correct |
29 |
Correct |
4 ms |
2432 KB |
Output is correct |
30 |
Correct |
4 ms |
2432 KB |
Output is correct |
31 |
Correct |
3 ms |
2148 KB |
Output is correct |
32 |
Correct |
4 ms |
2560 KB |
Output is correct |
33 |
Correct |
6 ms |
3968 KB |
Output is correct |
34 |
Correct |
4 ms |
3712 KB |
Output is correct |
35 |
Correct |
4 ms |
2816 KB |
Output is correct |
36 |
Correct |
4 ms |
2048 KB |
Output is correct |
37 |
Correct |
3 ms |
1792 KB |
Output is correct |
38 |
Correct |
3 ms |
2300 KB |
Output is correct |
39 |
Correct |
4 ms |
2432 KB |
Output is correct |
40 |
Correct |
2 ms |
512 KB |
Output is correct |
41 |
Correct |
3 ms |
2304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
113 ms |
164248 KB |
Output is correct |
3 |
Correct |
121 ms |
163420 KB |
Output is correct |
4 |
Correct |
114 ms |
164280 KB |
Output is correct |
5 |
Correct |
112 ms |
164216 KB |
Output is correct |
6 |
Correct |
116 ms |
164088 KB |
Output is correct |
7 |
Correct |
114 ms |
163336 KB |
Output is correct |
8 |
Correct |
112 ms |
163484 KB |
Output is correct |
9 |
Correct |
110 ms |
162552 KB |
Output is correct |
10 |
Correct |
112 ms |
164224 KB |
Output is correct |
11 |
Correct |
112 ms |
164208 KB |
Output is correct |
12 |
Correct |
32 ms |
44416 KB |
Output is correct |
13 |
Correct |
58 ms |
77824 KB |
Output is correct |
14 |
Correct |
81 ms |
112248 KB |
Output is correct |
15 |
Correct |
115 ms |
164216 KB |
Output is correct |
16 |
Correct |
112 ms |
164088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
556 KB |
Output is correct |
15 |
Correct |
2 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
2432 KB |
Output is correct |
19 |
Correct |
3 ms |
2048 KB |
Output is correct |
20 |
Correct |
3 ms |
2432 KB |
Output is correct |
21 |
Correct |
4 ms |
2560 KB |
Output is correct |
22 |
Correct |
3 ms |
1792 KB |
Output is correct |
23 |
Correct |
4 ms |
2304 KB |
Output is correct |
24 |
Correct |
3 ms |
1792 KB |
Output is correct |
25 |
Correct |
5 ms |
3968 KB |
Output is correct |
26 |
Correct |
4 ms |
4224 KB |
Output is correct |
27 |
Correct |
5 ms |
3200 KB |
Output is correct |
28 |
Correct |
4 ms |
2432 KB |
Output is correct |
29 |
Correct |
4 ms |
2432 KB |
Output is correct |
30 |
Correct |
4 ms |
2432 KB |
Output is correct |
31 |
Correct |
3 ms |
2148 KB |
Output is correct |
32 |
Correct |
4 ms |
2560 KB |
Output is correct |
33 |
Correct |
6 ms |
3968 KB |
Output is correct |
34 |
Correct |
4 ms |
3712 KB |
Output is correct |
35 |
Correct |
4 ms |
2816 KB |
Output is correct |
36 |
Correct |
4 ms |
2048 KB |
Output is correct |
37 |
Correct |
3 ms |
1792 KB |
Output is correct |
38 |
Correct |
3 ms |
2300 KB |
Output is correct |
39 |
Correct |
4 ms |
2432 KB |
Output is correct |
40 |
Correct |
2 ms |
512 KB |
Output is correct |
41 |
Correct |
3 ms |
2304 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
113 ms |
164248 KB |
Output is correct |
44 |
Correct |
121 ms |
163420 KB |
Output is correct |
45 |
Correct |
114 ms |
164280 KB |
Output is correct |
46 |
Correct |
112 ms |
164216 KB |
Output is correct |
47 |
Correct |
116 ms |
164088 KB |
Output is correct |
48 |
Correct |
114 ms |
163336 KB |
Output is correct |
49 |
Correct |
112 ms |
163484 KB |
Output is correct |
50 |
Correct |
110 ms |
162552 KB |
Output is correct |
51 |
Correct |
112 ms |
164224 KB |
Output is correct |
52 |
Correct |
112 ms |
164208 KB |
Output is correct |
53 |
Correct |
32 ms |
44416 KB |
Output is correct |
54 |
Correct |
58 ms |
77824 KB |
Output is correct |
55 |
Correct |
81 ms |
112248 KB |
Output is correct |
56 |
Correct |
115 ms |
164216 KB |
Output is correct |
57 |
Correct |
112 ms |
164088 KB |
Output is correct |
58 |
Correct |
198 ms |
120772 KB |
Output is correct |
59 |
Correct |
211 ms |
138168 KB |
Output is correct |
60 |
Correct |
204 ms |
130112 KB |
Output is correct |
61 |
Correct |
209 ms |
128380 KB |
Output is correct |
62 |
Correct |
110 ms |
163676 KB |
Output is correct |
63 |
Correct |
114 ms |
164984 KB |
Output is correct |
64 |
Correct |
151 ms |
164648 KB |
Output is correct |
65 |
Correct |
163 ms |
159196 KB |
Output is correct |
66 |
Correct |
208 ms |
128464 KB |
Output is correct |
67 |
Correct |
214 ms |
125816 KB |
Output is correct |
68 |
Correct |
226 ms |
131448 KB |
Output is correct |
69 |
Correct |
214 ms |
129184 KB |
Output is correct |
70 |
Correct |
210 ms |
134904 KB |
Output is correct |
71 |
Correct |
195 ms |
126456 KB |
Output is correct |
72 |
Correct |
43 ms |
49784 KB |
Output is correct |
73 |
Correct |
6 ms |
5504 KB |
Output is correct |