#include <bits/stdc++.h>
using namespace std;
const int MAXN = 405;
using lint = long long;
using pi = pair<int, int>;
int n;
char str[MAXN];
int dp[222][222][222][4];
vector<int> rep[3];
int sum[3][MAXN];
int cntAfter(char arg, int pos){
if(arg == 'R') return sum[0][pos + 1];
if(arg == 'G') return sum[1][pos + 1];
if(arg == 'Y') return sum[2][pos + 1];
assert(0);
}
int cost(int cR, int cG, int cY, int mode){
int pos = -1;
if(mode == 0) pos = rep[mode][cR-1];
if(mode == 1) pos = rep[mode][cG-1];
if(mode == 2) pos = rep[mode][cY-1];
return
max(cntAfter('R', pos) - cR, 0) +
max(cntAfter('G', pos) - cG, 0) +
max(cntAfter('Y', pos) - cY, 0);
}
int f(int cR, int cG, int cY, int prv){
if(cR + cG + cY == 0) return 0;
if(~dp[cR][cG][cY][prv]) return dp[cR][cG][cY][prv];
int ret = 1e9;
if(cR > 0 && prv != 0){
ret = min(ret, f(cR - 1, cG, cY, 0) + cost(cR, cG, cY, 0));
}
if(cG > 0 && prv != 1){
ret = min(ret, f(cR, cG - 1, cY, 1) + cost(cR, cG, cY, 1));
}
if(cY > 0 && prv != 2){
ret = min(ret, f(cR, cG, cY - 1, 2) + cost(cR, cG, cY, 2));
}
return dp[cR][cG][cY][prv] = ret;
}
int main(){
cin >> n >> str;
memset(dp, -1, sizeof(dp));
int cntr = count(str, str + n, 'R');
int cntg = count(str, str + n, 'G');
int cnty = count(str, str + n, 'Y');
if(max({cntr, cntg, cnty}) > 202){
puts("-1");
return 0;
}
for(int i=n-1; i>=0; i--){
if(str[i] == 'R'){
rep[0].push_back(i);
sum[0][i]++;
}
if(str[i] == 'G'){
rep[1].push_back(i);
sum[1][i]++;
}
if(str[i] == 'Y'){
rep[2].push_back(i);
sum[2][i]++;
}
for(int j=0; j<3; j++) sum[j][i] += sum[j][i+1];
}
int ans = f(cntr, cntg, cnty, 3);
if(ans > 1e8) ans = -1;
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
162 ms |
171640 KB |
Output is correct |
2 |
Correct |
143 ms |
171612 KB |
Output is correct |
3 |
Correct |
147 ms |
171568 KB |
Output is correct |
4 |
Correct |
150 ms |
171640 KB |
Output is correct |
5 |
Correct |
144 ms |
171640 KB |
Output is correct |
6 |
Correct |
160 ms |
171640 KB |
Output is correct |
7 |
Correct |
146 ms |
171636 KB |
Output is correct |
8 |
Correct |
136 ms |
171540 KB |
Output is correct |
9 |
Correct |
137 ms |
171560 KB |
Output is correct |
10 |
Correct |
146 ms |
171668 KB |
Output is correct |
11 |
Correct |
156 ms |
171768 KB |
Output is correct |
12 |
Correct |
139 ms |
171740 KB |
Output is correct |
13 |
Correct |
155 ms |
171772 KB |
Output is correct |
14 |
Correct |
158 ms |
171652 KB |
Output is correct |
15 |
Correct |
142 ms |
171612 KB |
Output is correct |
16 |
Correct |
143 ms |
171640 KB |
Output is correct |
17 |
Correct |
144 ms |
171628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
162 ms |
171640 KB |
Output is correct |
2 |
Correct |
143 ms |
171612 KB |
Output is correct |
3 |
Correct |
147 ms |
171568 KB |
Output is correct |
4 |
Correct |
150 ms |
171640 KB |
Output is correct |
5 |
Correct |
144 ms |
171640 KB |
Output is correct |
6 |
Correct |
160 ms |
171640 KB |
Output is correct |
7 |
Correct |
146 ms |
171636 KB |
Output is correct |
8 |
Correct |
136 ms |
171540 KB |
Output is correct |
9 |
Correct |
137 ms |
171560 KB |
Output is correct |
10 |
Correct |
146 ms |
171668 KB |
Output is correct |
11 |
Correct |
156 ms |
171768 KB |
Output is correct |
12 |
Correct |
139 ms |
171740 KB |
Output is correct |
13 |
Correct |
155 ms |
171772 KB |
Output is correct |
14 |
Correct |
158 ms |
171652 KB |
Output is correct |
15 |
Correct |
142 ms |
171612 KB |
Output is correct |
16 |
Correct |
143 ms |
171640 KB |
Output is correct |
17 |
Correct |
144 ms |
171628 KB |
Output is correct |
18 |
Correct |
138 ms |
171556 KB |
Output is correct |
19 |
Correct |
136 ms |
171684 KB |
Output is correct |
20 |
Correct |
135 ms |
171640 KB |
Output is correct |
21 |
Correct |
144 ms |
171584 KB |
Output is correct |
22 |
Correct |
142 ms |
171640 KB |
Output is correct |
23 |
Correct |
169 ms |
171640 KB |
Output is correct |
24 |
Correct |
145 ms |
171640 KB |
Output is correct |
25 |
Correct |
150 ms |
171604 KB |
Output is correct |
26 |
Correct |
135 ms |
171640 KB |
Output is correct |
27 |
Correct |
141 ms |
171768 KB |
Output is correct |
28 |
Correct |
149 ms |
171708 KB |
Output is correct |
29 |
Correct |
164 ms |
171656 KB |
Output is correct |
30 |
Correct |
139 ms |
171640 KB |
Output is correct |
31 |
Correct |
147 ms |
171760 KB |
Output is correct |
32 |
Correct |
138 ms |
171640 KB |
Output is correct |
33 |
Correct |
139 ms |
171640 KB |
Output is correct |
34 |
Correct |
136 ms |
171528 KB |
Output is correct |
35 |
Correct |
150 ms |
171640 KB |
Output is correct |
36 |
Correct |
136 ms |
171752 KB |
Output is correct |
37 |
Correct |
140 ms |
171744 KB |
Output is correct |
38 |
Correct |
142 ms |
171620 KB |
Output is correct |
39 |
Correct |
139 ms |
171724 KB |
Output is correct |
40 |
Correct |
159 ms |
171744 KB |
Output is correct |
41 |
Correct |
136 ms |
171644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
145 ms |
171688 KB |
Output is correct |
2 |
Correct |
145 ms |
171632 KB |
Output is correct |
3 |
Correct |
144 ms |
171644 KB |
Output is correct |
4 |
Correct |
162 ms |
171552 KB |
Output is correct |
5 |
Correct |
138 ms |
171604 KB |
Output is correct |
6 |
Correct |
203 ms |
171640 KB |
Output is correct |
7 |
Correct |
148 ms |
171764 KB |
Output is correct |
8 |
Correct |
147 ms |
171720 KB |
Output is correct |
9 |
Correct |
141 ms |
171640 KB |
Output is correct |
10 |
Correct |
143 ms |
171640 KB |
Output is correct |
11 |
Correct |
154 ms |
171640 KB |
Output is correct |
12 |
Correct |
148 ms |
171648 KB |
Output is correct |
13 |
Correct |
137 ms |
171612 KB |
Output is correct |
14 |
Correct |
142 ms |
171640 KB |
Output is correct |
15 |
Correct |
144 ms |
171556 KB |
Output is correct |
16 |
Correct |
145 ms |
171520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
162 ms |
171640 KB |
Output is correct |
2 |
Correct |
143 ms |
171612 KB |
Output is correct |
3 |
Correct |
147 ms |
171568 KB |
Output is correct |
4 |
Correct |
150 ms |
171640 KB |
Output is correct |
5 |
Correct |
144 ms |
171640 KB |
Output is correct |
6 |
Correct |
160 ms |
171640 KB |
Output is correct |
7 |
Correct |
146 ms |
171636 KB |
Output is correct |
8 |
Correct |
136 ms |
171540 KB |
Output is correct |
9 |
Correct |
137 ms |
171560 KB |
Output is correct |
10 |
Correct |
146 ms |
171668 KB |
Output is correct |
11 |
Correct |
156 ms |
171768 KB |
Output is correct |
12 |
Correct |
139 ms |
171740 KB |
Output is correct |
13 |
Correct |
155 ms |
171772 KB |
Output is correct |
14 |
Correct |
158 ms |
171652 KB |
Output is correct |
15 |
Correct |
142 ms |
171612 KB |
Output is correct |
16 |
Correct |
143 ms |
171640 KB |
Output is correct |
17 |
Correct |
144 ms |
171628 KB |
Output is correct |
18 |
Correct |
138 ms |
171556 KB |
Output is correct |
19 |
Correct |
136 ms |
171684 KB |
Output is correct |
20 |
Correct |
135 ms |
171640 KB |
Output is correct |
21 |
Correct |
144 ms |
171584 KB |
Output is correct |
22 |
Correct |
142 ms |
171640 KB |
Output is correct |
23 |
Correct |
169 ms |
171640 KB |
Output is correct |
24 |
Correct |
145 ms |
171640 KB |
Output is correct |
25 |
Correct |
150 ms |
171604 KB |
Output is correct |
26 |
Correct |
135 ms |
171640 KB |
Output is correct |
27 |
Correct |
141 ms |
171768 KB |
Output is correct |
28 |
Correct |
149 ms |
171708 KB |
Output is correct |
29 |
Correct |
164 ms |
171656 KB |
Output is correct |
30 |
Correct |
139 ms |
171640 KB |
Output is correct |
31 |
Correct |
147 ms |
171760 KB |
Output is correct |
32 |
Correct |
138 ms |
171640 KB |
Output is correct |
33 |
Correct |
139 ms |
171640 KB |
Output is correct |
34 |
Correct |
136 ms |
171528 KB |
Output is correct |
35 |
Correct |
150 ms |
171640 KB |
Output is correct |
36 |
Correct |
136 ms |
171752 KB |
Output is correct |
37 |
Correct |
140 ms |
171744 KB |
Output is correct |
38 |
Correct |
142 ms |
171620 KB |
Output is correct |
39 |
Correct |
139 ms |
171724 KB |
Output is correct |
40 |
Correct |
159 ms |
171744 KB |
Output is correct |
41 |
Correct |
136 ms |
171644 KB |
Output is correct |
42 |
Correct |
145 ms |
171688 KB |
Output is correct |
43 |
Correct |
145 ms |
171632 KB |
Output is correct |
44 |
Correct |
144 ms |
171644 KB |
Output is correct |
45 |
Correct |
162 ms |
171552 KB |
Output is correct |
46 |
Correct |
138 ms |
171604 KB |
Output is correct |
47 |
Correct |
203 ms |
171640 KB |
Output is correct |
48 |
Correct |
148 ms |
171764 KB |
Output is correct |
49 |
Correct |
147 ms |
171720 KB |
Output is correct |
50 |
Correct |
141 ms |
171640 KB |
Output is correct |
51 |
Correct |
143 ms |
171640 KB |
Output is correct |
52 |
Correct |
154 ms |
171640 KB |
Output is correct |
53 |
Correct |
148 ms |
171648 KB |
Output is correct |
54 |
Correct |
137 ms |
171612 KB |
Output is correct |
55 |
Correct |
142 ms |
171640 KB |
Output is correct |
56 |
Correct |
144 ms |
171556 KB |
Output is correct |
57 |
Correct |
145 ms |
171520 KB |
Output is correct |
58 |
Correct |
386 ms |
171740 KB |
Output is correct |
59 |
Correct |
324 ms |
171600 KB |
Output is correct |
60 |
Correct |
296 ms |
171656 KB |
Output is correct |
61 |
Correct |
308 ms |
171664 KB |
Output is correct |
62 |
Correct |
139 ms |
171584 KB |
Output is correct |
63 |
Correct |
147 ms |
171612 KB |
Output is correct |
64 |
Correct |
185 ms |
171552 KB |
Output is correct |
65 |
Correct |
176 ms |
171648 KB |
Output is correct |
66 |
Correct |
327 ms |
171672 KB |
Output is correct |
67 |
Correct |
271 ms |
171608 KB |
Output is correct |
68 |
Correct |
300 ms |
171628 KB |
Output is correct |
69 |
Correct |
303 ms |
171596 KB |
Output is correct |
70 |
Correct |
272 ms |
171680 KB |
Output is correct |
71 |
Correct |
297 ms |
171668 KB |
Output is correct |
72 |
Correct |
153 ms |
171632 KB |
Output is correct |
73 |
Correct |
151 ms |
171552 KB |
Output is correct |