#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX = 407;
int N;
int dp[MX][MX][MX][3];
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 itg = upper_bound(G.begin(), G.end(), R[r + 1]) - G.begin();
int ity = upper_bound(Y.begin(), Y.end(), R[r + 1]) - Y.begin();
res = min(res, f(r + 1, g, y, 0) + max(0, itg - g - 1) + max(0, ity - y - 1));
}
if(c != 1 && g + 1 < G.size()) {
int itr = upper_bound(R.begin(), R.end(), G[g + 1]) - R.begin();
int ity = upper_bound(Y.begin(), Y.end(), G[g + 1]) - Y.begin();
res = min(res, f(r, g + 1, y, 1) + max(0, itr - r - 1) + max(0, ity - y - 1));
}
if(c != 2 && y + 1 < Y.size()) {
int itr = upper_bound(R.begin(), R.end(), Y[y + 1]) - R.begin();
int itg = upper_bound(G.begin(), G.end(), Y[y + 1]) - G.begin();
res = min(res, f(r, g, y + 1, 2) + max(0, itr - r - 1) + max(0, itg - g - 1));
}
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);
}
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:25:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | if(c != 1 && g + 1 < G.size()) {
| ~~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:31:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | if(c != 2 && y + 1 < Y.size()) {
| ~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
294 ms |
791884 KB |
Output is correct |
2 |
Correct |
244 ms |
791844 KB |
Output is correct |
3 |
Correct |
246 ms |
791812 KB |
Output is correct |
4 |
Correct |
237 ms |
791804 KB |
Output is correct |
5 |
Correct |
243 ms |
791796 KB |
Output is correct |
6 |
Correct |
239 ms |
791856 KB |
Output is correct |
7 |
Correct |
237 ms |
791788 KB |
Output is correct |
8 |
Correct |
233 ms |
791860 KB |
Output is correct |
9 |
Correct |
239 ms |
791936 KB |
Output is correct |
10 |
Correct |
233 ms |
791884 KB |
Output is correct |
11 |
Correct |
250 ms |
791840 KB |
Output is correct |
12 |
Correct |
240 ms |
792016 KB |
Output is correct |
13 |
Correct |
236 ms |
791884 KB |
Output is correct |
14 |
Correct |
248 ms |
791880 KB |
Output is correct |
15 |
Correct |
235 ms |
791888 KB |
Output is correct |
16 |
Correct |
235 ms |
791804 KB |
Output is correct |
17 |
Correct |
239 ms |
791884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
294 ms |
791884 KB |
Output is correct |
2 |
Correct |
244 ms |
791844 KB |
Output is correct |
3 |
Correct |
246 ms |
791812 KB |
Output is correct |
4 |
Correct |
237 ms |
791804 KB |
Output is correct |
5 |
Correct |
243 ms |
791796 KB |
Output is correct |
6 |
Correct |
239 ms |
791856 KB |
Output is correct |
7 |
Correct |
237 ms |
791788 KB |
Output is correct |
8 |
Correct |
233 ms |
791860 KB |
Output is correct |
9 |
Correct |
239 ms |
791936 KB |
Output is correct |
10 |
Correct |
233 ms |
791884 KB |
Output is correct |
11 |
Correct |
250 ms |
791840 KB |
Output is correct |
12 |
Correct |
240 ms |
792016 KB |
Output is correct |
13 |
Correct |
236 ms |
791884 KB |
Output is correct |
14 |
Correct |
248 ms |
791880 KB |
Output is correct |
15 |
Correct |
235 ms |
791888 KB |
Output is correct |
16 |
Correct |
235 ms |
791804 KB |
Output is correct |
17 |
Correct |
239 ms |
791884 KB |
Output is correct |
18 |
Correct |
240 ms |
791884 KB |
Output is correct |
19 |
Correct |
247 ms |
791884 KB |
Output is correct |
20 |
Correct |
247 ms |
791800 KB |
Output is correct |
21 |
Correct |
237 ms |
791896 KB |
Output is correct |
22 |
Correct |
240 ms |
791860 KB |
Output is correct |
23 |
Correct |
237 ms |
791804 KB |
Output is correct |
24 |
Correct |
243 ms |
791888 KB |
Output is correct |
25 |
Correct |
262 ms |
791828 KB |
Output is correct |
26 |
Correct |
238 ms |
791888 KB |
Output is correct |
27 |
Correct |
240 ms |
792000 KB |
Output is correct |
28 |
Correct |
238 ms |
791916 KB |
Output is correct |
29 |
Correct |
236 ms |
791908 KB |
Output is correct |
30 |
Correct |
236 ms |
791832 KB |
Output is correct |
31 |
Correct |
251 ms |
791872 KB |
Output is correct |
32 |
Correct |
240 ms |
791824 KB |
Output is correct |
33 |
Correct |
236 ms |
791800 KB |
Output is correct |
34 |
Correct |
242 ms |
791800 KB |
Output is correct |
35 |
Correct |
238 ms |
791920 KB |
Output is correct |
36 |
Correct |
240 ms |
791908 KB |
Output is correct |
37 |
Correct |
235 ms |
791804 KB |
Output is correct |
38 |
Correct |
240 ms |
791884 KB |
Output is correct |
39 |
Correct |
240 ms |
791924 KB |
Output is correct |
40 |
Correct |
237 ms |
791884 KB |
Output is correct |
41 |
Correct |
239 ms |
791968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
235 ms |
791824 KB |
Output is correct |
2 |
Correct |
244 ms |
791864 KB |
Output is correct |
3 |
Correct |
246 ms |
792012 KB |
Output is correct |
4 |
Correct |
241 ms |
791916 KB |
Output is correct |
5 |
Correct |
237 ms |
791896 KB |
Output is correct |
6 |
Correct |
238 ms |
791928 KB |
Output is correct |
7 |
Correct |
238 ms |
791872 KB |
Output is correct |
8 |
Correct |
240 ms |
791952 KB |
Output is correct |
9 |
Correct |
250 ms |
791892 KB |
Output is correct |
10 |
Correct |
241 ms |
791916 KB |
Output is correct |
11 |
Correct |
236 ms |
791884 KB |
Output is correct |
12 |
Correct |
254 ms |
791928 KB |
Output is correct |
13 |
Correct |
238 ms |
791872 KB |
Output is correct |
14 |
Correct |
235 ms |
791956 KB |
Output is correct |
15 |
Correct |
239 ms |
791892 KB |
Output is correct |
16 |
Correct |
263 ms |
791944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
294 ms |
791884 KB |
Output is correct |
2 |
Correct |
244 ms |
791844 KB |
Output is correct |
3 |
Correct |
246 ms |
791812 KB |
Output is correct |
4 |
Correct |
237 ms |
791804 KB |
Output is correct |
5 |
Correct |
243 ms |
791796 KB |
Output is correct |
6 |
Correct |
239 ms |
791856 KB |
Output is correct |
7 |
Correct |
237 ms |
791788 KB |
Output is correct |
8 |
Correct |
233 ms |
791860 KB |
Output is correct |
9 |
Correct |
239 ms |
791936 KB |
Output is correct |
10 |
Correct |
233 ms |
791884 KB |
Output is correct |
11 |
Correct |
250 ms |
791840 KB |
Output is correct |
12 |
Correct |
240 ms |
792016 KB |
Output is correct |
13 |
Correct |
236 ms |
791884 KB |
Output is correct |
14 |
Correct |
248 ms |
791880 KB |
Output is correct |
15 |
Correct |
235 ms |
791888 KB |
Output is correct |
16 |
Correct |
235 ms |
791804 KB |
Output is correct |
17 |
Correct |
239 ms |
791884 KB |
Output is correct |
18 |
Correct |
240 ms |
791884 KB |
Output is correct |
19 |
Correct |
247 ms |
791884 KB |
Output is correct |
20 |
Correct |
247 ms |
791800 KB |
Output is correct |
21 |
Correct |
237 ms |
791896 KB |
Output is correct |
22 |
Correct |
240 ms |
791860 KB |
Output is correct |
23 |
Correct |
237 ms |
791804 KB |
Output is correct |
24 |
Correct |
243 ms |
791888 KB |
Output is correct |
25 |
Correct |
262 ms |
791828 KB |
Output is correct |
26 |
Correct |
238 ms |
791888 KB |
Output is correct |
27 |
Correct |
240 ms |
792000 KB |
Output is correct |
28 |
Correct |
238 ms |
791916 KB |
Output is correct |
29 |
Correct |
236 ms |
791908 KB |
Output is correct |
30 |
Correct |
236 ms |
791832 KB |
Output is correct |
31 |
Correct |
251 ms |
791872 KB |
Output is correct |
32 |
Correct |
240 ms |
791824 KB |
Output is correct |
33 |
Correct |
236 ms |
791800 KB |
Output is correct |
34 |
Correct |
242 ms |
791800 KB |
Output is correct |
35 |
Correct |
238 ms |
791920 KB |
Output is correct |
36 |
Correct |
240 ms |
791908 KB |
Output is correct |
37 |
Correct |
235 ms |
791804 KB |
Output is correct |
38 |
Correct |
240 ms |
791884 KB |
Output is correct |
39 |
Correct |
240 ms |
791924 KB |
Output is correct |
40 |
Correct |
237 ms |
791884 KB |
Output is correct |
41 |
Correct |
239 ms |
791968 KB |
Output is correct |
42 |
Correct |
235 ms |
791824 KB |
Output is correct |
43 |
Correct |
244 ms |
791864 KB |
Output is correct |
44 |
Correct |
246 ms |
792012 KB |
Output is correct |
45 |
Correct |
241 ms |
791916 KB |
Output is correct |
46 |
Correct |
237 ms |
791896 KB |
Output is correct |
47 |
Correct |
238 ms |
791928 KB |
Output is correct |
48 |
Correct |
238 ms |
791872 KB |
Output is correct |
49 |
Correct |
240 ms |
791952 KB |
Output is correct |
50 |
Correct |
250 ms |
791892 KB |
Output is correct |
51 |
Correct |
241 ms |
791916 KB |
Output is correct |
52 |
Correct |
236 ms |
791884 KB |
Output is correct |
53 |
Correct |
254 ms |
791928 KB |
Output is correct |
54 |
Correct |
238 ms |
791872 KB |
Output is correct |
55 |
Correct |
235 ms |
791956 KB |
Output is correct |
56 |
Correct |
239 ms |
791892 KB |
Output is correct |
57 |
Correct |
263 ms |
791944 KB |
Output is correct |
58 |
Execution timed out |
554 ms |
791856 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |