#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 405;
int N;
string s;
vector < vector < int > > dp[MX][4];
int cnt[MX][3]; //R = 0, G = 1, Y = 2
vector<int> R, G, Y;
int f(int r, int g, int y, int l) {
if(r + g + y == N) return 0;
if(dp[r][l][g][y] != -1) return dp[r][l][g][y];
int res = 1e9;
if(r < R.size() && l != 0) {
int cost = max(0, cnt[R[r]][1] - g) + max(0, cnt[R[r]][2] - y);
res = min(res, f(r + 1, g, y, 0) + cost);
}
if(g < G.size() && l != 1) {
int cost = max(0, cnt[G[g]][0] - r) + max(0, cnt[G[g]][2] - y);
res = min(res, f(r, g + 1, y, 1) + cost);
}
if(y < Y.size() && l != 2) {
int cost = max(0, cnt[Y[y]][0] - r) + max(0, cnt[Y[y]][1] - g);
res = min(res, f(r, g, y + 1, 2) + cost);
}
// cout << r << " " << g << " " << y << " = " << res << '\n';
return dp[r][l][g][y] = res;
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> N >> s;
for(int i = 0; i < N; i++) {
if(s[i] == 'R') {
R.push_back(i);
cnt[i][0]++;
}
if(s[i] == 'G') {
G.push_back(i);
cnt[i][1]++;
}
if(s[i] == 'Y') {
Y.push_back(i);
cnt[i][2]++;
}
if(i > 0) {
cnt[i][0] += cnt[i - 1][0];
cnt[i][1] += cnt[i - 1][1];
cnt[i][2] += cnt[i - 1][2];
}
}
for(int i = 0; i < MX; i++)
for(int c = 0; c < 4; c++)
dp[i][c].assign(G.size() + 1, vector<int>(Y.size() + 1, -1));
int k = f(0, 0, 0, 3);
if(k == 1e9) k = -1;
cout << k << '\n';
}
Compilation message
joi2019_ho_t3.cpp: In function 'int f(int, int, int, int)':
joi2019_ho_t3.cpp:20:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | if(r < R.size() && l != 0) {
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:24:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | if(g < G.size() && l != 1) {
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:28:12: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if(y < Y.size() && l != 2) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
2 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
2 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
3924 KB |
Output is correct |
19 |
Correct |
3 ms |
4180 KB |
Output is correct |
20 |
Correct |
2 ms |
3540 KB |
Output is correct |
21 |
Correct |
3 ms |
4692 KB |
Output is correct |
22 |
Correct |
3 ms |
2900 KB |
Output is correct |
23 |
Correct |
3 ms |
4692 KB |
Output is correct |
24 |
Correct |
4 ms |
6100 KB |
Output is correct |
25 |
Correct |
2 ms |
2388 KB |
Output is correct |
26 |
Correct |
3 ms |
2900 KB |
Output is correct |
27 |
Correct |
3 ms |
3540 KB |
Output is correct |
28 |
Correct |
3 ms |
4308 KB |
Output is correct |
29 |
Correct |
3 ms |
3924 KB |
Output is correct |
30 |
Correct |
5 ms |
5076 KB |
Output is correct |
31 |
Correct |
4 ms |
5460 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
5 ms |
3300 KB |
Output is correct |
34 |
Correct |
3 ms |
3460 KB |
Output is correct |
35 |
Correct |
3 ms |
3552 KB |
Output is correct |
36 |
Correct |
4 ms |
3924 KB |
Output is correct |
37 |
Correct |
5 ms |
5268 KB |
Output is correct |
38 |
Correct |
4 ms |
5588 KB |
Output is correct |
39 |
Correct |
4 ms |
4692 KB |
Output is correct |
40 |
Correct |
6 ms |
6996 KB |
Output is correct |
41 |
Correct |
7 ms |
5524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
15 ms |
18252 KB |
Output is correct |
3 |
Correct |
15 ms |
18052 KB |
Output is correct |
4 |
Correct |
15 ms |
18132 KB |
Output is correct |
5 |
Correct |
16 ms |
18132 KB |
Output is correct |
6 |
Correct |
18 ms |
18292 KB |
Output is correct |
7 |
Correct |
16 ms |
18172 KB |
Output is correct |
8 |
Correct |
15 ms |
18244 KB |
Output is correct |
9 |
Correct |
16 ms |
18132 KB |
Output is correct |
10 |
Correct |
16 ms |
18140 KB |
Output is correct |
11 |
Correct |
19 ms |
18252 KB |
Output is correct |
12 |
Correct |
8 ms |
9556 KB |
Output is correct |
13 |
Correct |
10 ms |
12620 KB |
Output is correct |
14 |
Correct |
13 ms |
15152 KB |
Output is correct |
15 |
Correct |
16 ms |
18276 KB |
Output is correct |
16 |
Correct |
16 ms |
18260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
2 ms |
980 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
1 ms |
724 KB |
Output is correct |
14 |
Correct |
1 ms |
852 KB |
Output is correct |
15 |
Correct |
1 ms |
852 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
3924 KB |
Output is correct |
19 |
Correct |
3 ms |
4180 KB |
Output is correct |
20 |
Correct |
2 ms |
3540 KB |
Output is correct |
21 |
Correct |
3 ms |
4692 KB |
Output is correct |
22 |
Correct |
3 ms |
2900 KB |
Output is correct |
23 |
Correct |
3 ms |
4692 KB |
Output is correct |
24 |
Correct |
4 ms |
6100 KB |
Output is correct |
25 |
Correct |
2 ms |
2388 KB |
Output is correct |
26 |
Correct |
3 ms |
2900 KB |
Output is correct |
27 |
Correct |
3 ms |
3540 KB |
Output is correct |
28 |
Correct |
3 ms |
4308 KB |
Output is correct |
29 |
Correct |
3 ms |
3924 KB |
Output is correct |
30 |
Correct |
5 ms |
5076 KB |
Output is correct |
31 |
Correct |
4 ms |
5460 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
5 ms |
3300 KB |
Output is correct |
34 |
Correct |
3 ms |
3460 KB |
Output is correct |
35 |
Correct |
3 ms |
3552 KB |
Output is correct |
36 |
Correct |
4 ms |
3924 KB |
Output is correct |
37 |
Correct |
5 ms |
5268 KB |
Output is correct |
38 |
Correct |
4 ms |
5588 KB |
Output is correct |
39 |
Correct |
4 ms |
4692 KB |
Output is correct |
40 |
Correct |
6 ms |
6996 KB |
Output is correct |
41 |
Correct |
7 ms |
5524 KB |
Output is correct |
42 |
Correct |
1 ms |
468 KB |
Output is correct |
43 |
Correct |
15 ms |
18252 KB |
Output is correct |
44 |
Correct |
15 ms |
18052 KB |
Output is correct |
45 |
Correct |
15 ms |
18132 KB |
Output is correct |
46 |
Correct |
16 ms |
18132 KB |
Output is correct |
47 |
Correct |
18 ms |
18292 KB |
Output is correct |
48 |
Correct |
16 ms |
18172 KB |
Output is correct |
49 |
Correct |
15 ms |
18244 KB |
Output is correct |
50 |
Correct |
16 ms |
18132 KB |
Output is correct |
51 |
Correct |
16 ms |
18140 KB |
Output is correct |
52 |
Correct |
19 ms |
18252 KB |
Output is correct |
53 |
Correct |
8 ms |
9556 KB |
Output is correct |
54 |
Correct |
10 ms |
12620 KB |
Output is correct |
55 |
Correct |
13 ms |
15152 KB |
Output is correct |
56 |
Correct |
16 ms |
18276 KB |
Output is correct |
57 |
Correct |
16 ms |
18260 KB |
Output is correct |
58 |
Correct |
147 ms |
131880 KB |
Output is correct |
59 |
Correct |
144 ms |
114932 KB |
Output is correct |
60 |
Correct |
140 ms |
112380 KB |
Output is correct |
61 |
Correct |
143 ms |
122600 KB |
Output is correct |
62 |
Correct |
15 ms |
16596 KB |
Output is correct |
63 |
Correct |
22 ms |
23000 KB |
Output is correct |
64 |
Correct |
44 ms |
52888 KB |
Output is correct |
65 |
Correct |
75 ms |
77408 KB |
Output is correct |
66 |
Correct |
139 ms |
118332 KB |
Output is correct |
67 |
Correct |
150 ms |
142024 KB |
Output is correct |
68 |
Correct |
141 ms |
118516 KB |
Output is correct |
69 |
Correct |
153 ms |
120744 KB |
Output is correct |
70 |
Correct |
162 ms |
120960 KB |
Output is correct |
71 |
Correct |
136 ms |
106784 KB |
Output is correct |
72 |
Correct |
11 ms |
14932 KB |
Output is correct |
73 |
Correct |
87 ms |
205220 KB |
Output is correct |