#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MX = 203;
int n;
int dp[MX][MX][MX][3];
vector<int>id[3]; //R G Y
int solve(int r, int g, int y, int prev) {
if(r+g+y==n) return 0;
int &ret = dp[r][g][y][prev];
if(ret!=-1) return ret;
ret = INF;
auto fix = [&] (int &a) {
a = max(0, a);
};
if(r+g+y==0 || prev!=0) {
if(id[0].size()>r) {
int cost1 = upper_bound(id[1].begin(), id[1].end(), id[0][r]) - id[1].begin();
cost1 -= g;
int cost2 = upper_bound(id[2].begin(), id[2].end(), id[0][r]) - id[2].begin();
cost2 -= y;
fix(cost1);
fix(cost2);
ret = min(ret, cost1+cost2+solve(r+1, g, y, 0));
}
}
if(prev!=1 && id[1].size()>g) {
int cost1 = upper_bound(id[0].begin(), id[0].end(), id[1][g]) - id[0].begin();
cost1 -= r;
int cost2 = upper_bound(id[2].begin(), id[2].end(), id[1][g]) - id[2].begin();
cost2 -= y;
fix(cost1);
fix(cost2);
ret = min(ret, cost1+cost2+solve(r, g+1, y, 1));
}
if(prev!=2 && id[2].size()>y) {
int cost1 = upper_bound(id[0].begin(), id[0].end(), id[2][y]) - id[0].begin();
cost1 -= r;
int cost2 = upper_bound(id[1].begin(), id[1].end(), id[2][y]) - id[1].begin();
cost2 -= g;
fix(cost1);
fix(cost2);
ret = min(ret, cost1+cost2+solve(r, g, y+1, 2));
}
return ret;
}
void PlayGround() {
string s;
cin>>n>>s;
for(int i=0; i<n; ++i) {
if(s[i]=='R') id[0].push_back(i);
else if(s[i]=='G') id[1].push_back(i);
else id[2].push_back(i);
}
int mx = max({id[0].size(), id[1].size(), id[2].size()});
if((n+1)/2<mx) {
cout<<"-1\n";
return;
}
memset(dp, -1, sizeof dp);
cout<<solve(0, 0, 0, 0)<<'\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
PlayGround();
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int solve(int, int, int, int)':
joi2019_ho_t3.cpp:25:20: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
25 | if(id[0].size()>r) {
| ~~~~~~~~~~~~^~
joi2019_ho_t3.cpp:35:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
35 | if(prev!=1 && id[1].size()>g) {
| ~~~~~~~~~~~~^~
joi2019_ho_t3.cpp:44:29: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
44 | if(prev!=2 && id[2].size()>y) {
| ~~~~~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
98436 KB |
Output is correct |
2 |
Correct |
40 ms |
98508 KB |
Output is correct |
3 |
Correct |
38 ms |
98444 KB |
Output is correct |
4 |
Correct |
42 ms |
98496 KB |
Output is correct |
5 |
Correct |
38 ms |
98412 KB |
Output is correct |
6 |
Correct |
37 ms |
98508 KB |
Output is correct |
7 |
Correct |
38 ms |
98544 KB |
Output is correct |
8 |
Correct |
37 ms |
98448 KB |
Output is correct |
9 |
Correct |
39 ms |
98472 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
37 ms |
98464 KB |
Output is correct |
12 |
Correct |
37 ms |
98488 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
37 ms |
98464 KB |
Output is correct |
15 |
Correct |
37 ms |
98448 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
98436 KB |
Output is correct |
2 |
Correct |
40 ms |
98508 KB |
Output is correct |
3 |
Correct |
38 ms |
98444 KB |
Output is correct |
4 |
Correct |
42 ms |
98496 KB |
Output is correct |
5 |
Correct |
38 ms |
98412 KB |
Output is correct |
6 |
Correct |
37 ms |
98508 KB |
Output is correct |
7 |
Correct |
38 ms |
98544 KB |
Output is correct |
8 |
Correct |
37 ms |
98448 KB |
Output is correct |
9 |
Correct |
39 ms |
98472 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
37 ms |
98464 KB |
Output is correct |
12 |
Correct |
37 ms |
98488 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
37 ms |
98464 KB |
Output is correct |
15 |
Correct |
37 ms |
98448 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
38 ms |
98504 KB |
Output is correct |
19 |
Correct |
37 ms |
98484 KB |
Output is correct |
20 |
Correct |
38 ms |
98412 KB |
Output is correct |
21 |
Correct |
39 ms |
98476 KB |
Output is correct |
22 |
Correct |
38 ms |
98464 KB |
Output is correct |
23 |
Correct |
38 ms |
98444 KB |
Output is correct |
24 |
Correct |
38 ms |
98580 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
41 ms |
98496 KB |
Output is correct |
28 |
Correct |
39 ms |
98496 KB |
Output is correct |
29 |
Correct |
38 ms |
98408 KB |
Output is correct |
30 |
Correct |
39 ms |
98452 KB |
Output is correct |
31 |
Correct |
40 ms |
98512 KB |
Output is correct |
32 |
Correct |
38 ms |
98452 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
38 ms |
98520 KB |
Output is correct |
36 |
Correct |
38 ms |
98508 KB |
Output is correct |
37 |
Correct |
38 ms |
98488 KB |
Output is correct |
38 |
Correct |
39 ms |
98508 KB |
Output is correct |
39 |
Correct |
42 ms |
98412 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
98508 KB |
Output is correct |
2 |
Correct |
41 ms |
98508 KB |
Output is correct |
3 |
Correct |
38 ms |
98508 KB |
Output is correct |
4 |
Correct |
44 ms |
98568 KB |
Output is correct |
5 |
Correct |
38 ms |
98508 KB |
Output is correct |
6 |
Correct |
39 ms |
98508 KB |
Output is correct |
7 |
Correct |
39 ms |
98460 KB |
Output is correct |
8 |
Correct |
37 ms |
98508 KB |
Output is correct |
9 |
Correct |
38 ms |
98476 KB |
Output is correct |
10 |
Correct |
38 ms |
98472 KB |
Output is correct |
11 |
Correct |
38 ms |
98500 KB |
Output is correct |
12 |
Correct |
38 ms |
98492 KB |
Output is correct |
13 |
Correct |
38 ms |
98500 KB |
Output is correct |
14 |
Correct |
38 ms |
98508 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
98436 KB |
Output is correct |
2 |
Correct |
40 ms |
98508 KB |
Output is correct |
3 |
Correct |
38 ms |
98444 KB |
Output is correct |
4 |
Correct |
42 ms |
98496 KB |
Output is correct |
5 |
Correct |
38 ms |
98412 KB |
Output is correct |
6 |
Correct |
37 ms |
98508 KB |
Output is correct |
7 |
Correct |
38 ms |
98544 KB |
Output is correct |
8 |
Correct |
37 ms |
98448 KB |
Output is correct |
9 |
Correct |
39 ms |
98472 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
37 ms |
98464 KB |
Output is correct |
12 |
Correct |
37 ms |
98488 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
37 ms |
98464 KB |
Output is correct |
15 |
Correct |
37 ms |
98448 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
38 ms |
98504 KB |
Output is correct |
19 |
Correct |
37 ms |
98484 KB |
Output is correct |
20 |
Correct |
38 ms |
98412 KB |
Output is correct |
21 |
Correct |
39 ms |
98476 KB |
Output is correct |
22 |
Correct |
38 ms |
98464 KB |
Output is correct |
23 |
Correct |
38 ms |
98444 KB |
Output is correct |
24 |
Correct |
38 ms |
98580 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
41 ms |
98496 KB |
Output is correct |
28 |
Correct |
39 ms |
98496 KB |
Output is correct |
29 |
Correct |
38 ms |
98408 KB |
Output is correct |
30 |
Correct |
39 ms |
98452 KB |
Output is correct |
31 |
Correct |
40 ms |
98512 KB |
Output is correct |
32 |
Correct |
38 ms |
98452 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
38 ms |
98520 KB |
Output is correct |
36 |
Correct |
38 ms |
98508 KB |
Output is correct |
37 |
Correct |
38 ms |
98488 KB |
Output is correct |
38 |
Correct |
39 ms |
98508 KB |
Output is correct |
39 |
Correct |
42 ms |
98412 KB |
Output is correct |
40 |
Correct |
0 ms |
212 KB |
Output is correct |
41 |
Correct |
0 ms |
328 KB |
Output is correct |
42 |
Correct |
38 ms |
98508 KB |
Output is correct |
43 |
Correct |
41 ms |
98508 KB |
Output is correct |
44 |
Correct |
38 ms |
98508 KB |
Output is correct |
45 |
Correct |
44 ms |
98568 KB |
Output is correct |
46 |
Correct |
38 ms |
98508 KB |
Output is correct |
47 |
Correct |
39 ms |
98508 KB |
Output is correct |
48 |
Correct |
39 ms |
98460 KB |
Output is correct |
49 |
Correct |
37 ms |
98508 KB |
Output is correct |
50 |
Correct |
38 ms |
98476 KB |
Output is correct |
51 |
Correct |
38 ms |
98472 KB |
Output is correct |
52 |
Correct |
38 ms |
98500 KB |
Output is correct |
53 |
Correct |
38 ms |
98492 KB |
Output is correct |
54 |
Correct |
38 ms |
98500 KB |
Output is correct |
55 |
Correct |
38 ms |
98508 KB |
Output is correct |
56 |
Correct |
1 ms |
212 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
382 ms |
98568 KB |
Output is correct |
59 |
Correct |
394 ms |
98568 KB |
Output is correct |
60 |
Correct |
393 ms |
98568 KB |
Output is correct |
61 |
Correct |
408 ms |
98572 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
42 ms |
98560 KB |
Output is correct |
64 |
Correct |
81 ms |
98564 KB |
Output is correct |
65 |
Correct |
155 ms |
98568 KB |
Output is correct |
66 |
Correct |
218 ms |
98508 KB |
Output is correct |
67 |
Correct |
199 ms |
98516 KB |
Output is correct |
68 |
Correct |
232 ms |
98560 KB |
Output is correct |
69 |
Correct |
236 ms |
98560 KB |
Output is correct |
70 |
Correct |
260 ms |
98508 KB |
Output is correct |
71 |
Correct |
292 ms |
98632 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |