#include <bits/stdc++.h>
#define iii tuple<int, int, int>
using namespace std;
const int N = 405;
int n;
int cnt[3], dp[3][N][N][N];
char A[N];
vector<iii> vec[3];
int f(char c) {
if(c == 'R') return 0;
if(c == 'G') return 1;
return 2;
}
int cost(int c, iii a, iii b) {
int ret = 0;
if(c != 0) ret += max(0, get<0>(b) - get<0>(a));
if(c != 1) ret += max(0, get<1>(b) - get<1>(a));
if(c != 2) ret += max(0, get<2>(b) - get<2>(a));
return ret;
}
int main() {
scanf("%d %s", &n, A + 1);
for(int i = 1; i <= n; i++) {
++cnt[f(A[i])];
vec[f(A[i])].emplace_back(cnt[0], cnt[1], cnt[2]);
}
int R = cnt[0], G = cnt[1], Y = cnt[2];
for(int i = 0; i <= R; i++) for(int j = 0; j <= G; j++) for(int k = 0; k <= Y; k++) {
if(!i && !j && !k) continue;
dp[0][i][j][k] = dp[1][i][j][k] = dp[2][i][j][k] = 1e9;
if(i) dp[0][i][j][k] = min(dp[1][i-1][j][k], dp[2][i-1][j][k]) + cost(0, vec[0][i-1], {i, j, k});
if(j) dp[1][i][j][k] = min(dp[0][i][j-1][k], dp[2][i][j-1][k]) + cost(1, vec[1][j-1], {i, j, k});
if(k) dp[2][i][j][k] = min(dp[0][i][j][k-1], dp[1][i][j][k-1]) + cost(2, vec[2][k-1], {i, j, k});
}
int ans = 1e9;
for(int i = 0; i < 3; i++) ans = min(ans, dp[i][R][G][Y]);
if(ans == 1e9) printf("-1\n");
else printf("%d\n", ans);
return 0;
}
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, A + 1);
~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
640 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
640 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
5 ms |
2304 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
7 ms |
2048 KB |
Output is correct |
23 |
Correct |
5 ms |
2432 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
6 ms |
4736 KB |
Output is correct |
26 |
Correct |
7 ms |
5120 KB |
Output is correct |
27 |
Correct |
6 ms |
3712 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
6 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2304 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
4736 KB |
Output is correct |
34 |
Correct |
6 ms |
4224 KB |
Output is correct |
35 |
Correct |
6 ms |
3200 KB |
Output is correct |
36 |
Correct |
5 ms |
2304 KB |
Output is correct |
37 |
Correct |
5 ms |
2048 KB |
Output is correct |
38 |
Correct |
5 ms |
2432 KB |
Output is correct |
39 |
Correct |
6 ms |
2816 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
2560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
86 ms |
194296 KB |
Output is correct |
3 |
Correct |
86 ms |
193276 KB |
Output is correct |
4 |
Correct |
86 ms |
194296 KB |
Output is correct |
5 |
Correct |
91 ms |
194296 KB |
Output is correct |
6 |
Correct |
102 ms |
194312 KB |
Output is correct |
7 |
Correct |
89 ms |
193272 KB |
Output is correct |
8 |
Correct |
92 ms |
193400 KB |
Output is correct |
9 |
Correct |
87 ms |
192376 KB |
Output is correct |
10 |
Correct |
94 ms |
194296 KB |
Output is correct |
11 |
Correct |
92 ms |
194296 KB |
Output is correct |
12 |
Correct |
29 ms |
52856 KB |
Output is correct |
13 |
Correct |
45 ms |
92280 KB |
Output is correct |
14 |
Correct |
63 ms |
132856 KB |
Output is correct |
15 |
Correct |
90 ms |
194296 KB |
Output is correct |
16 |
Correct |
93 ms |
194296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
5 ms |
768 KB |
Output is correct |
11 |
Correct |
5 ms |
640 KB |
Output is correct |
12 |
Correct |
5 ms |
640 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
512 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
2688 KB |
Output is correct |
19 |
Correct |
5 ms |
2304 KB |
Output is correct |
20 |
Correct |
6 ms |
2688 KB |
Output is correct |
21 |
Correct |
6 ms |
2816 KB |
Output is correct |
22 |
Correct |
7 ms |
2048 KB |
Output is correct |
23 |
Correct |
5 ms |
2432 KB |
Output is correct |
24 |
Correct |
5 ms |
1920 KB |
Output is correct |
25 |
Correct |
6 ms |
4736 KB |
Output is correct |
26 |
Correct |
7 ms |
5120 KB |
Output is correct |
27 |
Correct |
6 ms |
3712 KB |
Output is correct |
28 |
Correct |
6 ms |
2816 KB |
Output is correct |
29 |
Correct |
6 ms |
2688 KB |
Output is correct |
30 |
Correct |
6 ms |
2688 KB |
Output is correct |
31 |
Correct |
6 ms |
2304 KB |
Output is correct |
32 |
Correct |
6 ms |
2816 KB |
Output is correct |
33 |
Correct |
6 ms |
4736 KB |
Output is correct |
34 |
Correct |
6 ms |
4224 KB |
Output is correct |
35 |
Correct |
6 ms |
3200 KB |
Output is correct |
36 |
Correct |
5 ms |
2304 KB |
Output is correct |
37 |
Correct |
5 ms |
2048 KB |
Output is correct |
38 |
Correct |
5 ms |
2432 KB |
Output is correct |
39 |
Correct |
6 ms |
2816 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
2560 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |
43 |
Correct |
86 ms |
194296 KB |
Output is correct |
44 |
Correct |
86 ms |
193276 KB |
Output is correct |
45 |
Correct |
86 ms |
194296 KB |
Output is correct |
46 |
Correct |
91 ms |
194296 KB |
Output is correct |
47 |
Correct |
102 ms |
194312 KB |
Output is correct |
48 |
Correct |
89 ms |
193272 KB |
Output is correct |
49 |
Correct |
92 ms |
193400 KB |
Output is correct |
50 |
Correct |
87 ms |
192376 KB |
Output is correct |
51 |
Correct |
94 ms |
194296 KB |
Output is correct |
52 |
Correct |
92 ms |
194296 KB |
Output is correct |
53 |
Correct |
29 ms |
52856 KB |
Output is correct |
54 |
Correct |
45 ms |
92280 KB |
Output is correct |
55 |
Correct |
63 ms |
132856 KB |
Output is correct |
56 |
Correct |
90 ms |
194296 KB |
Output is correct |
57 |
Correct |
93 ms |
194296 KB |
Output is correct |
58 |
Correct |
51 ms |
77176 KB |
Output is correct |
59 |
Correct |
61 ms |
97784 KB |
Output is correct |
60 |
Correct |
57 ms |
88952 KB |
Output is correct |
61 |
Correct |
55 ms |
86008 KB |
Output is correct |
62 |
Correct |
88 ms |
189432 KB |
Output is correct |
63 |
Correct |
105 ms |
186616 KB |
Output is correct |
64 |
Correct |
87 ms |
163064 KB |
Output is correct |
65 |
Correct |
76 ms |
141944 KB |
Output is correct |
66 |
Correct |
56 ms |
87232 KB |
Output is correct |
67 |
Correct |
54 ms |
83832 KB |
Output is correct |
68 |
Correct |
56 ms |
89848 KB |
Output is correct |
69 |
Correct |
68 ms |
86648 KB |
Output is correct |
70 |
Correct |
58 ms |
93692 KB |
Output is correct |
71 |
Correct |
56 ms |
85240 KB |
Output is correct |
72 |
Correct |
28 ms |
44032 KB |
Output is correct |
73 |
Correct |
6 ms |
2944 KB |
Output is correct |