#include <bits/stdc++.h>
using i64 = long long;
#define all(x) (x).begin(), (x).end()
const int N = 400 + 5;
const int INF = 1E9;
int n, pre[3][N][3];
std::vector<int> idxs[3];
std::vector<std::vector<std::vector<std::vector<int>>>> dp;
std::vector<int>::iterator it;
int f(int a, int b, int c, int last) {
if(dp[a][b][c][last] != -1) {
return dp[a][b][c][last];
} else if(a + b + c == n) {
return 0;
}
int res = INF;
if(a != idxs[0].size() && last != 0) {
int cost = 0;
cost += std::max(0, pre[0][a][1] - b);
cost += std::max(0, pre[0][a][2] - c);
res = std::min(res, f(a + 1, b, c, 0) + cost);
}
if(b != idxs[1].size() && last != 1) {
int cost = 0;
cost += std::max(0, pre[1][b][0] - a);
cost += std::max(0, pre[1][b][2] - c);
res = std::min(res, f(a, b + 1, c, 1) + cost);
}
if(c != idxs[2].size() && last != 2) {
int cost = 0;
cost += std::max(0, pre[2][c][0] - a);
cost += std::max(0, pre[2][c][1] - b);
res = std::min(res, f(a, b, c + 1, 2) + cost);
}
return dp[a][b][c][last] = res;
}
void solve() {
std::string s;
std::cin >> n >> s;
for(int i = 0; i < n; i++) {
if(s[i] == 'R') {
idxs[0].emplace_back(i);
} else if(s[i] == 'G') {
idxs[1].emplace_back(i);
} else {
idxs[2].emplace_back(i);
}
}
dp.assign(idxs[0].size() + 1, std::vector (idxs[1].size() + 1, std::vector (idxs[2].size() + 1, std::vector (4, -1))));
for(int i = 0; i < idxs[0].size(); i++) {
pre[0][i][1] = std::upper_bound(all(idxs[1]), idxs[0][i]) - idxs[1].begin();
pre[0][i][2] = std::upper_bound(all(idxs[2]), idxs[0][i]) - idxs[2].begin();
}
for(int i = 0; i < idxs[1].size(); i++) {
pre[1][i][0] = std::upper_bound(all(idxs[0]), idxs[1][i]) - idxs[0].begin();
pre[1][i][2] = std::upper_bound(all(idxs[2]), idxs[1][i]) - idxs[2].begin();
}
for(int i = 0; i < idxs[2].size(); i++) {
pre[2][i][0] = std::upper_bound(all(idxs[0]), idxs[2][i]) - idxs[0].begin();
pre[2][i][1] = std::upper_bound(all(idxs[1]), idxs[2][i]) - idxs[1].begin();
}
int ans = f(0, 0, 0, 3);
if(ans != INF) {
std::cout << ans;
} else {
std::cout << -1;
}
return;
}
signed main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL); std::cout.tie(NULL);
int t = 1; //std::cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int f(int, int, int, int)':
joi2019_ho_t3.cpp:22:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | if(a != idxs[0].size() && last != 0) {
| ~~^~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:30:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | if(b != idxs[1].size() && last != 1) {
| ~~^~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:38:7: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | if(c != idxs[2].size() && last != 2) {
| ~~^~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:65:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | for(int i = 0; i < idxs[0].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:70:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i = 0; i < idxs[1].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:75:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for(int i = 0; i < idxs[2].size(); i++) {
| ~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
856 KB |
Output is correct |
19 |
Correct |
1 ms |
856 KB |
Output is correct |
20 |
Correct |
1 ms |
856 KB |
Output is correct |
21 |
Correct |
1 ms |
856 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
856 KB |
Output is correct |
24 |
Correct |
1 ms |
1112 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
856 KB |
Output is correct |
30 |
Correct |
1 ms |
856 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
856 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
856 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
1 ms |
856 KB |
Output is correct |
40 |
Correct |
1 ms |
548 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
4184 KB |
Output is correct |
3 |
Correct |
4 ms |
4184 KB |
Output is correct |
4 |
Correct |
4 ms |
3932 KB |
Output is correct |
5 |
Correct |
4 ms |
3928 KB |
Output is correct |
6 |
Correct |
4 ms |
3928 KB |
Output is correct |
7 |
Correct |
4 ms |
3928 KB |
Output is correct |
8 |
Correct |
3 ms |
3928 KB |
Output is correct |
9 |
Correct |
4 ms |
3928 KB |
Output is correct |
10 |
Correct |
4 ms |
3928 KB |
Output is correct |
11 |
Correct |
4 ms |
3928 KB |
Output is correct |
12 |
Correct |
1 ms |
1368 KB |
Output is correct |
13 |
Correct |
2 ms |
2136 KB |
Output is correct |
14 |
Correct |
3 ms |
2652 KB |
Output is correct |
15 |
Correct |
4 ms |
3928 KB |
Output is correct |
16 |
Correct |
4 ms |
3932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
856 KB |
Output is correct |
19 |
Correct |
1 ms |
856 KB |
Output is correct |
20 |
Correct |
1 ms |
856 KB |
Output is correct |
21 |
Correct |
1 ms |
856 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
856 KB |
Output is correct |
24 |
Correct |
1 ms |
1112 KB |
Output is correct |
25 |
Correct |
0 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
856 KB |
Output is correct |
30 |
Correct |
1 ms |
856 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
856 KB |
Output is correct |
33 |
Correct |
1 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
600 KB |
Output is correct |
35 |
Correct |
1 ms |
856 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
856 KB |
Output is correct |
38 |
Correct |
1 ms |
856 KB |
Output is correct |
39 |
Correct |
1 ms |
856 KB |
Output is correct |
40 |
Correct |
1 ms |
548 KB |
Output is correct |
41 |
Correct |
1 ms |
600 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
4 ms |
4184 KB |
Output is correct |
44 |
Correct |
4 ms |
4184 KB |
Output is correct |
45 |
Correct |
4 ms |
3932 KB |
Output is correct |
46 |
Correct |
4 ms |
3928 KB |
Output is correct |
47 |
Correct |
4 ms |
3928 KB |
Output is correct |
48 |
Correct |
4 ms |
3928 KB |
Output is correct |
49 |
Correct |
3 ms |
3928 KB |
Output is correct |
50 |
Correct |
4 ms |
3928 KB |
Output is correct |
51 |
Correct |
4 ms |
3928 KB |
Output is correct |
52 |
Correct |
4 ms |
3928 KB |
Output is correct |
53 |
Correct |
1 ms |
1368 KB |
Output is correct |
54 |
Correct |
2 ms |
2136 KB |
Output is correct |
55 |
Correct |
3 ms |
2652 KB |
Output is correct |
56 |
Correct |
4 ms |
3928 KB |
Output is correct |
57 |
Correct |
4 ms |
3932 KB |
Output is correct |
58 |
Correct |
261 ms |
133456 KB |
Output is correct |
59 |
Correct |
253 ms |
133464 KB |
Output is correct |
60 |
Correct |
249 ms |
132312 KB |
Output is correct |
61 |
Correct |
254 ms |
133756 KB |
Output is correct |
62 |
Correct |
12 ms |
12376 KB |
Output is correct |
63 |
Correct |
20 ms |
20824 KB |
Output is correct |
64 |
Correct |
82 ms |
66760 KB |
Output is correct |
65 |
Correct |
138 ms |
95568 KB |
Output is correct |
66 |
Correct |
257 ms |
131920 KB |
Output is correct |
67 |
Correct |
259 ms |
132140 KB |
Output is correct |
68 |
Correct |
234 ms |
133712 KB |
Output is correct |
69 |
Correct |
259 ms |
134992 KB |
Output is correct |
70 |
Correct |
243 ms |
134152 KB |
Output is correct |
71 |
Correct |
268 ms |
130980 KB |
Output is correct |
72 |
Correct |
36 ms |
34128 KB |
Output is correct |
73 |
Correct |
9 ms |
10584 KB |
Output is correct |