#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN = 410, M = 1e9+7;
int n, dp[4][210][210][210], sum[3][mxN], cnt[3];
string s;
vector<int> a[3];
int solvepos(char c, int pos){
if(c == 'R'){
return sum[0][pos+1];
}
if(c == 'G'){
return sum[1][pos+1];
}
return sum[2][pos+1];
}
int cost(int flag, int R, int G, int Y){
int pos;
if(flag == 0){
pos = a[flag][R-1];
}
if(flag == 1){
pos = a[flag][G-1];
}
if(flag == 2){
pos = a[flag][Y-1];
}
return max(solvepos('R', pos) - R, 0) + max(solvepos('G', pos) - G, 0) + max(solvepos('Y', pos) - Y, 0);
}
int solve(int last, int R, int G, int Y){
if(R+G+Y == 0){
return 0;
}
if(~dp[last][R][G][Y]) return dp[last][R][G][Y];
int flag = 1e9;
if(R && last != 0){
flag = min(flag, solve(0, R-1, G, Y) + cost(0, R, G, Y));
}
if(G && last != 1){
flag = min(flag, solve(1, R, G-1, Y) + cost(1, R, G, Y));
}
if(Y && last != 2){
flag = min(flag, solve(2, R, G, Y-1) + cost(2, R, G, Y));
}
return dp[last][R][G][Y] = flag;
}
int main(){
#ifdef _DEBUG
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0);
cin >> n >> s;
for(int i = n-1; i>=0; --i){
if(s[i] == 'R'){
cnt[0]++;
a[0].push_back(i);
sum[0][i]++;
}else if(s[i] == 'G'){
cnt[1]++;
a[1].push_back(i);
sum[1][i]++;
}else{
cnt[2]++;
a[2].push_back(i);
sum[2][i]++;
}
for(int j = 0; j<3; ++j){
sum[j][i] += sum[j][i+1];
}
}
memset(dp, -1, sizeof(dp));
if(max(cnt[0], max(cnt[1], cnt[2])) > 202){
cout << "-1\n";
}else{
int ans = solve(3, cnt[0], cnt[1], cnt[2]);
if(ans >= 1e8){
cout << "-1\n";
}else{
cout << ans << "\n";
}
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'int cost(int, int, int, int)':
joi2019_ho_t3.cpp:14:26: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
14 | return sum[0][pos+1];
| ~~~^~
joi2019_ho_t3.cpp:23:9: note: 'pos' was declared here
23 | int pos;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
145220 KB |
Output is correct |
2 |
Correct |
55 ms |
145280 KB |
Output is correct |
3 |
Correct |
55 ms |
145192 KB |
Output is correct |
4 |
Correct |
54 ms |
145220 KB |
Output is correct |
5 |
Correct |
58 ms |
145204 KB |
Output is correct |
6 |
Correct |
55 ms |
145212 KB |
Output is correct |
7 |
Correct |
56 ms |
145188 KB |
Output is correct |
8 |
Correct |
59 ms |
145204 KB |
Output is correct |
9 |
Correct |
55 ms |
145248 KB |
Output is correct |
10 |
Correct |
57 ms |
145192 KB |
Output is correct |
11 |
Correct |
57 ms |
145228 KB |
Output is correct |
12 |
Correct |
57 ms |
145216 KB |
Output is correct |
13 |
Correct |
55 ms |
145208 KB |
Output is correct |
14 |
Correct |
54 ms |
145220 KB |
Output is correct |
15 |
Correct |
55 ms |
145180 KB |
Output is correct |
16 |
Correct |
55 ms |
145228 KB |
Output is correct |
17 |
Correct |
57 ms |
145200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
145220 KB |
Output is correct |
2 |
Correct |
55 ms |
145280 KB |
Output is correct |
3 |
Correct |
55 ms |
145192 KB |
Output is correct |
4 |
Correct |
54 ms |
145220 KB |
Output is correct |
5 |
Correct |
58 ms |
145204 KB |
Output is correct |
6 |
Correct |
55 ms |
145212 KB |
Output is correct |
7 |
Correct |
56 ms |
145188 KB |
Output is correct |
8 |
Correct |
59 ms |
145204 KB |
Output is correct |
9 |
Correct |
55 ms |
145248 KB |
Output is correct |
10 |
Correct |
57 ms |
145192 KB |
Output is correct |
11 |
Correct |
57 ms |
145228 KB |
Output is correct |
12 |
Correct |
57 ms |
145216 KB |
Output is correct |
13 |
Correct |
55 ms |
145208 KB |
Output is correct |
14 |
Correct |
54 ms |
145220 KB |
Output is correct |
15 |
Correct |
55 ms |
145180 KB |
Output is correct |
16 |
Correct |
55 ms |
145228 KB |
Output is correct |
17 |
Correct |
57 ms |
145200 KB |
Output is correct |
18 |
Correct |
58 ms |
145204 KB |
Output is correct |
19 |
Correct |
55 ms |
145220 KB |
Output is correct |
20 |
Correct |
55 ms |
145220 KB |
Output is correct |
21 |
Correct |
56 ms |
145260 KB |
Output is correct |
22 |
Correct |
64 ms |
145280 KB |
Output is correct |
23 |
Correct |
55 ms |
145252 KB |
Output is correct |
24 |
Correct |
56 ms |
145160 KB |
Output is correct |
25 |
Correct |
55 ms |
145224 KB |
Output is correct |
26 |
Correct |
58 ms |
145228 KB |
Output is correct |
27 |
Correct |
56 ms |
145248 KB |
Output is correct |
28 |
Correct |
56 ms |
145220 KB |
Output is correct |
29 |
Correct |
57 ms |
145244 KB |
Output is correct |
30 |
Correct |
57 ms |
145224 KB |
Output is correct |
31 |
Correct |
56 ms |
145212 KB |
Output is correct |
32 |
Correct |
56 ms |
145224 KB |
Output is correct |
33 |
Correct |
58 ms |
145456 KB |
Output is correct |
34 |
Correct |
56 ms |
145196 KB |
Output is correct |
35 |
Correct |
57 ms |
145172 KB |
Output is correct |
36 |
Correct |
56 ms |
145224 KB |
Output is correct |
37 |
Correct |
58 ms |
145220 KB |
Output is correct |
38 |
Correct |
55 ms |
145176 KB |
Output is correct |
39 |
Correct |
55 ms |
145216 KB |
Output is correct |
40 |
Correct |
57 ms |
145212 KB |
Output is correct |
41 |
Correct |
55 ms |
145268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
145208 KB |
Output is correct |
2 |
Correct |
57 ms |
145276 KB |
Output is correct |
3 |
Correct |
54 ms |
145264 KB |
Output is correct |
4 |
Correct |
54 ms |
145192 KB |
Output is correct |
5 |
Correct |
55 ms |
145336 KB |
Output is correct |
6 |
Correct |
56 ms |
145252 KB |
Output is correct |
7 |
Correct |
55 ms |
145308 KB |
Output is correct |
8 |
Correct |
55 ms |
145268 KB |
Output is correct |
9 |
Correct |
55 ms |
145204 KB |
Output is correct |
10 |
Correct |
53 ms |
145192 KB |
Output is correct |
11 |
Correct |
55 ms |
145292 KB |
Output is correct |
12 |
Correct |
55 ms |
145216 KB |
Output is correct |
13 |
Correct |
55 ms |
145224 KB |
Output is correct |
14 |
Correct |
57 ms |
145292 KB |
Output is correct |
15 |
Correct |
56 ms |
145392 KB |
Output is correct |
16 |
Correct |
55 ms |
145220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
60 ms |
145220 KB |
Output is correct |
2 |
Correct |
55 ms |
145280 KB |
Output is correct |
3 |
Correct |
55 ms |
145192 KB |
Output is correct |
4 |
Correct |
54 ms |
145220 KB |
Output is correct |
5 |
Correct |
58 ms |
145204 KB |
Output is correct |
6 |
Correct |
55 ms |
145212 KB |
Output is correct |
7 |
Correct |
56 ms |
145188 KB |
Output is correct |
8 |
Correct |
59 ms |
145204 KB |
Output is correct |
9 |
Correct |
55 ms |
145248 KB |
Output is correct |
10 |
Correct |
57 ms |
145192 KB |
Output is correct |
11 |
Correct |
57 ms |
145228 KB |
Output is correct |
12 |
Correct |
57 ms |
145216 KB |
Output is correct |
13 |
Correct |
55 ms |
145208 KB |
Output is correct |
14 |
Correct |
54 ms |
145220 KB |
Output is correct |
15 |
Correct |
55 ms |
145180 KB |
Output is correct |
16 |
Correct |
55 ms |
145228 KB |
Output is correct |
17 |
Correct |
57 ms |
145200 KB |
Output is correct |
18 |
Correct |
58 ms |
145204 KB |
Output is correct |
19 |
Correct |
55 ms |
145220 KB |
Output is correct |
20 |
Correct |
55 ms |
145220 KB |
Output is correct |
21 |
Correct |
56 ms |
145260 KB |
Output is correct |
22 |
Correct |
64 ms |
145280 KB |
Output is correct |
23 |
Correct |
55 ms |
145252 KB |
Output is correct |
24 |
Correct |
56 ms |
145160 KB |
Output is correct |
25 |
Correct |
55 ms |
145224 KB |
Output is correct |
26 |
Correct |
58 ms |
145228 KB |
Output is correct |
27 |
Correct |
56 ms |
145248 KB |
Output is correct |
28 |
Correct |
56 ms |
145220 KB |
Output is correct |
29 |
Correct |
57 ms |
145244 KB |
Output is correct |
30 |
Correct |
57 ms |
145224 KB |
Output is correct |
31 |
Correct |
56 ms |
145212 KB |
Output is correct |
32 |
Correct |
56 ms |
145224 KB |
Output is correct |
33 |
Correct |
58 ms |
145456 KB |
Output is correct |
34 |
Correct |
56 ms |
145196 KB |
Output is correct |
35 |
Correct |
57 ms |
145172 KB |
Output is correct |
36 |
Correct |
56 ms |
145224 KB |
Output is correct |
37 |
Correct |
58 ms |
145220 KB |
Output is correct |
38 |
Correct |
55 ms |
145176 KB |
Output is correct |
39 |
Correct |
55 ms |
145216 KB |
Output is correct |
40 |
Correct |
57 ms |
145212 KB |
Output is correct |
41 |
Correct |
55 ms |
145268 KB |
Output is correct |
42 |
Correct |
55 ms |
145208 KB |
Output is correct |
43 |
Correct |
57 ms |
145276 KB |
Output is correct |
44 |
Correct |
54 ms |
145264 KB |
Output is correct |
45 |
Correct |
54 ms |
145192 KB |
Output is correct |
46 |
Correct |
55 ms |
145336 KB |
Output is correct |
47 |
Correct |
56 ms |
145252 KB |
Output is correct |
48 |
Correct |
55 ms |
145308 KB |
Output is correct |
49 |
Correct |
55 ms |
145268 KB |
Output is correct |
50 |
Correct |
55 ms |
145204 KB |
Output is correct |
51 |
Correct |
53 ms |
145192 KB |
Output is correct |
52 |
Correct |
55 ms |
145292 KB |
Output is correct |
53 |
Correct |
55 ms |
145216 KB |
Output is correct |
54 |
Correct |
55 ms |
145224 KB |
Output is correct |
55 |
Correct |
57 ms |
145292 KB |
Output is correct |
56 |
Correct |
56 ms |
145392 KB |
Output is correct |
57 |
Correct |
55 ms |
145220 KB |
Output is correct |
58 |
Correct |
149 ms |
145292 KB |
Output is correct |
59 |
Correct |
144 ms |
145432 KB |
Output is correct |
60 |
Correct |
147 ms |
145348 KB |
Output is correct |
61 |
Correct |
149 ms |
145320 KB |
Output is correct |
62 |
Correct |
55 ms |
145208 KB |
Output is correct |
63 |
Correct |
54 ms |
145284 KB |
Output is correct |
64 |
Correct |
74 ms |
145316 KB |
Output is correct |
65 |
Correct |
94 ms |
145328 KB |
Output is correct |
66 |
Correct |
162 ms |
145220 KB |
Output is correct |
67 |
Correct |
175 ms |
145316 KB |
Output is correct |
68 |
Correct |
148 ms |
145204 KB |
Output is correct |
69 |
Correct |
148 ms |
145436 KB |
Output is correct |
70 |
Correct |
148 ms |
145412 KB |
Output is correct |
71 |
Correct |
142 ms |
145292 KB |
Output is correct |
72 |
Correct |
55 ms |
145252 KB |
Output is correct |
73 |
Correct |
57 ms |
145220 KB |
Output is correct |