#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 407;
int N;
int dp[MX][MX][MX][3], lastR[MX], lastG[MX], lastY[MX];
vector<int> R, G, Y;
int f(int r, int g, int y, int c) {
int pos = r + g + y, res = 1e9;
if(dp[r][g][y][c] != -1) return dp[r][g][y][c];
if(pos == N) return dp[r][g][y][c] = 0;
if(c != 0 && r + 1 < R.size()) {
int cost = max(0, lastG[R[r + 1]] - g) + max(0, lastY[R[r + 1]] - y);
res = min(res, f(r + 1, g, y, 0) + cost);
}
if(c != 1 && g + 1 < G.size()) {
int cost = max(0, lastR[G[g + 1]] - r) + max(0, lastY[G[g + 1]] - y);
res = min(res, f(r, g + 1, y, 1) + cost);
}
if(c != 2 && y + 1 < Y.size()) {
int cost = max(0, lastR[Y[y + 1]] - r) + max(0, lastG[Y[y + 1]] - g);
res = min(res, f(r, g, y + 1, 2) + cost);
}
return dp[r][g][y][c] = res;
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
memset(dp, -1, sizeof dp);
cin >> N;
R = {0}, G = {0}, Y = {0};
for(int i = 1; i <= N; i++) {
char c;
cin >> c;
if(c == 'R') R.push_back(i);
if(c == 'G') G.push_back(i);
if(c == 'Y') Y.push_back(i);
lastR[i] = R.size() - 1;
lastG[i] = G.size() - 1;
lastY[i] = Y.size() - 1;
}
for(int r = 1; r < R.size(); r++) {
for(int g = 1; g < G.size(); g++) {
for(int y = 1; y < Y.size(); y++) {
}
}
}
int k = min({f(0, 0, 0, 0), f(0, 0, 0, 1), f(0, 0, 0, 2)});
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:18:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | if(c != 0 && r + 1 < R.size()) {
| ~~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:23:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | if(c != 1 && g + 1 < G.size()) {
| ~~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:28:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if(c != 2 && y + 1 < Y.size()) {
| ~~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:58:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int r = 1; r < R.size(); r++) {
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:59:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int g = 1; g < G.size(); g++) {
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:60:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int y = 1; y < Y.size(); y++) {
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
309 ms |
791892 KB |
Output is correct |
2 |
Correct |
234 ms |
791820 KB |
Output is correct |
3 |
Correct |
246 ms |
791832 KB |
Output is correct |
4 |
Correct |
242 ms |
791904 KB |
Output is correct |
5 |
Correct |
263 ms |
791836 KB |
Output is correct |
6 |
Correct |
232 ms |
791888 KB |
Output is correct |
7 |
Correct |
232 ms |
791968 KB |
Output is correct |
8 |
Correct |
233 ms |
791792 KB |
Output is correct |
9 |
Correct |
231 ms |
791872 KB |
Output is correct |
10 |
Correct |
237 ms |
791896 KB |
Output is correct |
11 |
Correct |
231 ms |
791796 KB |
Output is correct |
12 |
Correct |
234 ms |
791828 KB |
Output is correct |
13 |
Correct |
243 ms |
791888 KB |
Output is correct |
14 |
Correct |
231 ms |
791808 KB |
Output is correct |
15 |
Correct |
231 ms |
791792 KB |
Output is correct |
16 |
Correct |
237 ms |
791828 KB |
Output is correct |
17 |
Correct |
232 ms |
791824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
309 ms |
791892 KB |
Output is correct |
2 |
Correct |
234 ms |
791820 KB |
Output is correct |
3 |
Correct |
246 ms |
791832 KB |
Output is correct |
4 |
Correct |
242 ms |
791904 KB |
Output is correct |
5 |
Correct |
263 ms |
791836 KB |
Output is correct |
6 |
Correct |
232 ms |
791888 KB |
Output is correct |
7 |
Correct |
232 ms |
791968 KB |
Output is correct |
8 |
Correct |
233 ms |
791792 KB |
Output is correct |
9 |
Correct |
231 ms |
791872 KB |
Output is correct |
10 |
Correct |
237 ms |
791896 KB |
Output is correct |
11 |
Correct |
231 ms |
791796 KB |
Output is correct |
12 |
Correct |
234 ms |
791828 KB |
Output is correct |
13 |
Correct |
243 ms |
791888 KB |
Output is correct |
14 |
Correct |
231 ms |
791808 KB |
Output is correct |
15 |
Correct |
231 ms |
791792 KB |
Output is correct |
16 |
Correct |
237 ms |
791828 KB |
Output is correct |
17 |
Correct |
232 ms |
791824 KB |
Output is correct |
18 |
Correct |
244 ms |
791904 KB |
Output is correct |
19 |
Correct |
242 ms |
791876 KB |
Output is correct |
20 |
Correct |
243 ms |
791884 KB |
Output is correct |
21 |
Correct |
233 ms |
791912 KB |
Output is correct |
22 |
Correct |
235 ms |
791808 KB |
Output is correct |
23 |
Correct |
235 ms |
791836 KB |
Output is correct |
24 |
Correct |
234 ms |
791792 KB |
Output is correct |
25 |
Correct |
238 ms |
791820 KB |
Output is correct |
26 |
Correct |
265 ms |
791888 KB |
Output is correct |
27 |
Correct |
241 ms |
791852 KB |
Output is correct |
28 |
Correct |
231 ms |
791848 KB |
Output is correct |
29 |
Correct |
267 ms |
791816 KB |
Output is correct |
30 |
Correct |
241 ms |
791968 KB |
Output is correct |
31 |
Correct |
234 ms |
791836 KB |
Output is correct |
32 |
Correct |
265 ms |
791972 KB |
Output is correct |
33 |
Correct |
242 ms |
791960 KB |
Output is correct |
34 |
Correct |
232 ms |
791832 KB |
Output is correct |
35 |
Correct |
246 ms |
791828 KB |
Output is correct |
36 |
Correct |
246 ms |
791920 KB |
Output is correct |
37 |
Correct |
270 ms |
791796 KB |
Output is correct |
38 |
Correct |
237 ms |
791856 KB |
Output is correct |
39 |
Correct |
240 ms |
791860 KB |
Output is correct |
40 |
Correct |
238 ms |
791792 KB |
Output is correct |
41 |
Correct |
240 ms |
791848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
235 ms |
791808 KB |
Output is correct |
2 |
Correct |
233 ms |
791884 KB |
Output is correct |
3 |
Correct |
246 ms |
791828 KB |
Output is correct |
4 |
Correct |
237 ms |
791876 KB |
Output is correct |
5 |
Correct |
236 ms |
791860 KB |
Output is correct |
6 |
Correct |
251 ms |
791944 KB |
Output is correct |
7 |
Correct |
234 ms |
791832 KB |
Output is correct |
8 |
Correct |
233 ms |
791876 KB |
Output is correct |
9 |
Correct |
235 ms |
791880 KB |
Output is correct |
10 |
Correct |
253 ms |
791836 KB |
Output is correct |
11 |
Correct |
233 ms |
791940 KB |
Output is correct |
12 |
Correct |
234 ms |
791900 KB |
Output is correct |
13 |
Correct |
243 ms |
792032 KB |
Output is correct |
14 |
Correct |
240 ms |
791852 KB |
Output is correct |
15 |
Correct |
252 ms |
791872 KB |
Output is correct |
16 |
Correct |
237 ms |
791888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
309 ms |
791892 KB |
Output is correct |
2 |
Correct |
234 ms |
791820 KB |
Output is correct |
3 |
Correct |
246 ms |
791832 KB |
Output is correct |
4 |
Correct |
242 ms |
791904 KB |
Output is correct |
5 |
Correct |
263 ms |
791836 KB |
Output is correct |
6 |
Correct |
232 ms |
791888 KB |
Output is correct |
7 |
Correct |
232 ms |
791968 KB |
Output is correct |
8 |
Correct |
233 ms |
791792 KB |
Output is correct |
9 |
Correct |
231 ms |
791872 KB |
Output is correct |
10 |
Correct |
237 ms |
791896 KB |
Output is correct |
11 |
Correct |
231 ms |
791796 KB |
Output is correct |
12 |
Correct |
234 ms |
791828 KB |
Output is correct |
13 |
Correct |
243 ms |
791888 KB |
Output is correct |
14 |
Correct |
231 ms |
791808 KB |
Output is correct |
15 |
Correct |
231 ms |
791792 KB |
Output is correct |
16 |
Correct |
237 ms |
791828 KB |
Output is correct |
17 |
Correct |
232 ms |
791824 KB |
Output is correct |
18 |
Correct |
244 ms |
791904 KB |
Output is correct |
19 |
Correct |
242 ms |
791876 KB |
Output is correct |
20 |
Correct |
243 ms |
791884 KB |
Output is correct |
21 |
Correct |
233 ms |
791912 KB |
Output is correct |
22 |
Correct |
235 ms |
791808 KB |
Output is correct |
23 |
Correct |
235 ms |
791836 KB |
Output is correct |
24 |
Correct |
234 ms |
791792 KB |
Output is correct |
25 |
Correct |
238 ms |
791820 KB |
Output is correct |
26 |
Correct |
265 ms |
791888 KB |
Output is correct |
27 |
Correct |
241 ms |
791852 KB |
Output is correct |
28 |
Correct |
231 ms |
791848 KB |
Output is correct |
29 |
Correct |
267 ms |
791816 KB |
Output is correct |
30 |
Correct |
241 ms |
791968 KB |
Output is correct |
31 |
Correct |
234 ms |
791836 KB |
Output is correct |
32 |
Correct |
265 ms |
791972 KB |
Output is correct |
33 |
Correct |
242 ms |
791960 KB |
Output is correct |
34 |
Correct |
232 ms |
791832 KB |
Output is correct |
35 |
Correct |
246 ms |
791828 KB |
Output is correct |
36 |
Correct |
246 ms |
791920 KB |
Output is correct |
37 |
Correct |
270 ms |
791796 KB |
Output is correct |
38 |
Correct |
237 ms |
791856 KB |
Output is correct |
39 |
Correct |
240 ms |
791860 KB |
Output is correct |
40 |
Correct |
238 ms |
791792 KB |
Output is correct |
41 |
Correct |
240 ms |
791848 KB |
Output is correct |
42 |
Correct |
235 ms |
791808 KB |
Output is correct |
43 |
Correct |
233 ms |
791884 KB |
Output is correct |
44 |
Correct |
246 ms |
791828 KB |
Output is correct |
45 |
Correct |
237 ms |
791876 KB |
Output is correct |
46 |
Correct |
236 ms |
791860 KB |
Output is correct |
47 |
Correct |
251 ms |
791944 KB |
Output is correct |
48 |
Correct |
234 ms |
791832 KB |
Output is correct |
49 |
Correct |
233 ms |
791876 KB |
Output is correct |
50 |
Correct |
235 ms |
791880 KB |
Output is correct |
51 |
Correct |
253 ms |
791836 KB |
Output is correct |
52 |
Correct |
233 ms |
791940 KB |
Output is correct |
53 |
Correct |
234 ms |
791900 KB |
Output is correct |
54 |
Correct |
243 ms |
792032 KB |
Output is correct |
55 |
Correct |
240 ms |
791852 KB |
Output is correct |
56 |
Correct |
252 ms |
791872 KB |
Output is correct |
57 |
Correct |
237 ms |
791888 KB |
Output is correct |
58 |
Correct |
314 ms |
792052 KB |
Output is correct |
59 |
Correct |
327 ms |
791948 KB |
Output is correct |
60 |
Correct |
309 ms |
791848 KB |
Output is correct |
61 |
Correct |
355 ms |
791948 KB |
Output is correct |
62 |
Correct |
240 ms |
791880 KB |
Output is correct |
63 |
Correct |
241 ms |
792028 KB |
Output is correct |
64 |
Correct |
257 ms |
791848 KB |
Output is correct |
65 |
Correct |
344 ms |
791916 KB |
Output is correct |
66 |
Correct |
316 ms |
791900 KB |
Output is correct |
67 |
Correct |
311 ms |
791932 KB |
Output is correct |
68 |
Correct |
325 ms |
791932 KB |
Output is correct |
69 |
Correct |
319 ms |
791956 KB |
Output is correct |
70 |
Correct |
320 ms |
791948 KB |
Output is correct |
71 |
Correct |
312 ms |
791956 KB |
Output is correct |
72 |
Correct |
239 ms |
791912 KB |
Output is correct |
73 |
Correct |
242 ms |
791940 KB |
Output is correct |