#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define all(aa) aa.begin(), aa.end()
const int INF=1e8;
int n;
vector<int> ind[3], cnt[3];
int dp[402][402][402][4];
int f(array<int, 3> x, int l){
if(x[0]+x[1]+x[2]==n) return 0;
int &cur=dp[x[0]][x[1]][x[2]][l];
if(cur!=-1) return cur;
cur=INF;
for(int i=0; i<3; i++) if(i!=l && x[i]!=ind[i].size()){
int o=0, s=x[0]+x[1]+x[2];
for(int j=0; j<3; j++) if(j!=i)
o+=max(0, x[j]-cnt[j][ind[i][x[i]]]);
x[i]++;
cur=min(cur, f(x, i)+o+ind[i][x[i]-1]-s);
x[i]--;
}
return cur;
}
int main(){
string s;
cin>>n>>s;
for(int i=0; i<3; i++) cnt[i].resize(n);
for(int i=0; i<n; i++){
if(i!=0){
for(int j=0; j<3; j++){
cnt[j][i]=cnt[j][i-1];
}
}
if(s[i]=='R'){
ind[0].push_back(i);
cnt[0][i]++;
}
else if(s[i]=='G'){
ind[1].push_back(i);
cnt[1][i]++;
}
else{
ind[2].push_back(i);
cnt[2][i]++;
}
}
memset(dp, -1, sizeof(dp));
int ans=f(array<int, 3>{0, 0, 0}, 3);
cout<<(ans>=INF ? -1:ans)<<endl;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int f(std::array<int, 3>, int)':
joi2019_ho_t3.cpp:20:40: warning: comparison of integer expressions of different signedness: 'std::array<int, 3>::value_type' {aka 'int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
20 | for(int i=0; i<3; i++) if(i!=l && x[i]!=ind[i].size()){
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
460 ms |
1017464 KB |
Output is correct |
2 |
Correct |
293 ms |
1017268 KB |
Output is correct |
3 |
Correct |
264 ms |
1017424 KB |
Output is correct |
4 |
Correct |
266 ms |
1017432 KB |
Output is correct |
5 |
Correct |
263 ms |
1017424 KB |
Output is correct |
6 |
Correct |
261 ms |
1017532 KB |
Output is correct |
7 |
Correct |
259 ms |
1017464 KB |
Output is correct |
8 |
Correct |
262 ms |
1017488 KB |
Output is correct |
9 |
Correct |
265 ms |
1017268 KB |
Output is correct |
10 |
Correct |
250 ms |
1017432 KB |
Output is correct |
11 |
Correct |
253 ms |
1017364 KB |
Output is correct |
12 |
Correct |
259 ms |
1017308 KB |
Output is correct |
13 |
Correct |
249 ms |
1017392 KB |
Output is correct |
14 |
Correct |
248 ms |
1017324 KB |
Output is correct |
15 |
Correct |
246 ms |
1017428 KB |
Output is correct |
16 |
Correct |
245 ms |
1017496 KB |
Output is correct |
17 |
Correct |
242 ms |
1017420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
460 ms |
1017464 KB |
Output is correct |
2 |
Correct |
293 ms |
1017268 KB |
Output is correct |
3 |
Correct |
264 ms |
1017424 KB |
Output is correct |
4 |
Correct |
266 ms |
1017432 KB |
Output is correct |
5 |
Correct |
263 ms |
1017424 KB |
Output is correct |
6 |
Correct |
261 ms |
1017532 KB |
Output is correct |
7 |
Correct |
259 ms |
1017464 KB |
Output is correct |
8 |
Correct |
262 ms |
1017488 KB |
Output is correct |
9 |
Correct |
265 ms |
1017268 KB |
Output is correct |
10 |
Correct |
250 ms |
1017432 KB |
Output is correct |
11 |
Correct |
253 ms |
1017364 KB |
Output is correct |
12 |
Correct |
259 ms |
1017308 KB |
Output is correct |
13 |
Correct |
249 ms |
1017392 KB |
Output is correct |
14 |
Correct |
248 ms |
1017324 KB |
Output is correct |
15 |
Correct |
246 ms |
1017428 KB |
Output is correct |
16 |
Correct |
245 ms |
1017496 KB |
Output is correct |
17 |
Correct |
242 ms |
1017420 KB |
Output is correct |
18 |
Correct |
268 ms |
1017456 KB |
Output is correct |
19 |
Correct |
258 ms |
1017444 KB |
Output is correct |
20 |
Correct |
203 ms |
1017340 KB |
Output is correct |
21 |
Correct |
194 ms |
1017544 KB |
Output is correct |
22 |
Correct |
192 ms |
1017428 KB |
Output is correct |
23 |
Correct |
195 ms |
1017380 KB |
Output is correct |
24 |
Correct |
194 ms |
1017460 KB |
Output is correct |
25 |
Correct |
205 ms |
1017376 KB |
Output is correct |
26 |
Correct |
190 ms |
1017260 KB |
Output is correct |
27 |
Correct |
191 ms |
1017452 KB |
Output is correct |
28 |
Correct |
192 ms |
1017428 KB |
Output is correct |
29 |
Correct |
192 ms |
1017428 KB |
Output is correct |
30 |
Correct |
196 ms |
1017256 KB |
Output is correct |
31 |
Correct |
192 ms |
1017284 KB |
Output is correct |
32 |
Correct |
214 ms |
1017284 KB |
Output is correct |
33 |
Correct |
189 ms |
1017420 KB |
Output is correct |
34 |
Correct |
197 ms |
1017340 KB |
Output is correct |
35 |
Correct |
192 ms |
1017240 KB |
Output is correct |
36 |
Correct |
198 ms |
1017244 KB |
Output is correct |
37 |
Correct |
203 ms |
1017376 KB |
Output is correct |
38 |
Correct |
197 ms |
1017432 KB |
Output is correct |
39 |
Correct |
196 ms |
1017308 KB |
Output is correct |
40 |
Correct |
197 ms |
1017428 KB |
Output is correct |
41 |
Correct |
190 ms |
1017236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
205 ms |
1017456 KB |
Output is correct |
2 |
Correct |
185 ms |
1017448 KB |
Output is correct |
3 |
Correct |
191 ms |
1017428 KB |
Output is correct |
4 |
Correct |
180 ms |
1017424 KB |
Output is correct |
5 |
Correct |
182 ms |
1017428 KB |
Output is correct |
6 |
Correct |
189 ms |
1017424 KB |
Output is correct |
7 |
Correct |
193 ms |
1017292 KB |
Output is correct |
8 |
Correct |
186 ms |
1017288 KB |
Output is correct |
9 |
Correct |
189 ms |
1017296 KB |
Output is correct |
10 |
Correct |
194 ms |
1017288 KB |
Output is correct |
11 |
Correct |
216 ms |
1017328 KB |
Output is correct |
12 |
Correct |
225 ms |
1017364 KB |
Output is correct |
13 |
Correct |
195 ms |
1017424 KB |
Output is correct |
14 |
Correct |
185 ms |
1017420 KB |
Output is correct |
15 |
Correct |
188 ms |
1017480 KB |
Output is correct |
16 |
Correct |
187 ms |
1017336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
460 ms |
1017464 KB |
Output is correct |
2 |
Correct |
293 ms |
1017268 KB |
Output is correct |
3 |
Correct |
264 ms |
1017424 KB |
Output is correct |
4 |
Correct |
266 ms |
1017432 KB |
Output is correct |
5 |
Correct |
263 ms |
1017424 KB |
Output is correct |
6 |
Correct |
261 ms |
1017532 KB |
Output is correct |
7 |
Correct |
259 ms |
1017464 KB |
Output is correct |
8 |
Correct |
262 ms |
1017488 KB |
Output is correct |
9 |
Correct |
265 ms |
1017268 KB |
Output is correct |
10 |
Correct |
250 ms |
1017432 KB |
Output is correct |
11 |
Correct |
253 ms |
1017364 KB |
Output is correct |
12 |
Correct |
259 ms |
1017308 KB |
Output is correct |
13 |
Correct |
249 ms |
1017392 KB |
Output is correct |
14 |
Correct |
248 ms |
1017324 KB |
Output is correct |
15 |
Correct |
246 ms |
1017428 KB |
Output is correct |
16 |
Correct |
245 ms |
1017496 KB |
Output is correct |
17 |
Correct |
242 ms |
1017420 KB |
Output is correct |
18 |
Correct |
268 ms |
1017456 KB |
Output is correct |
19 |
Correct |
258 ms |
1017444 KB |
Output is correct |
20 |
Correct |
203 ms |
1017340 KB |
Output is correct |
21 |
Correct |
194 ms |
1017544 KB |
Output is correct |
22 |
Correct |
192 ms |
1017428 KB |
Output is correct |
23 |
Correct |
195 ms |
1017380 KB |
Output is correct |
24 |
Correct |
194 ms |
1017460 KB |
Output is correct |
25 |
Correct |
205 ms |
1017376 KB |
Output is correct |
26 |
Correct |
190 ms |
1017260 KB |
Output is correct |
27 |
Correct |
191 ms |
1017452 KB |
Output is correct |
28 |
Correct |
192 ms |
1017428 KB |
Output is correct |
29 |
Correct |
192 ms |
1017428 KB |
Output is correct |
30 |
Correct |
196 ms |
1017256 KB |
Output is correct |
31 |
Correct |
192 ms |
1017284 KB |
Output is correct |
32 |
Correct |
214 ms |
1017284 KB |
Output is correct |
33 |
Correct |
189 ms |
1017420 KB |
Output is correct |
34 |
Correct |
197 ms |
1017340 KB |
Output is correct |
35 |
Correct |
192 ms |
1017240 KB |
Output is correct |
36 |
Correct |
198 ms |
1017244 KB |
Output is correct |
37 |
Correct |
203 ms |
1017376 KB |
Output is correct |
38 |
Correct |
197 ms |
1017432 KB |
Output is correct |
39 |
Correct |
196 ms |
1017308 KB |
Output is correct |
40 |
Correct |
197 ms |
1017428 KB |
Output is correct |
41 |
Correct |
190 ms |
1017236 KB |
Output is correct |
42 |
Correct |
205 ms |
1017456 KB |
Output is correct |
43 |
Correct |
185 ms |
1017448 KB |
Output is correct |
44 |
Correct |
191 ms |
1017428 KB |
Output is correct |
45 |
Correct |
180 ms |
1017424 KB |
Output is correct |
46 |
Correct |
182 ms |
1017428 KB |
Output is correct |
47 |
Correct |
189 ms |
1017424 KB |
Output is correct |
48 |
Correct |
193 ms |
1017292 KB |
Output is correct |
49 |
Correct |
186 ms |
1017288 KB |
Output is correct |
50 |
Correct |
189 ms |
1017296 KB |
Output is correct |
51 |
Correct |
194 ms |
1017288 KB |
Output is correct |
52 |
Correct |
216 ms |
1017328 KB |
Output is correct |
53 |
Correct |
225 ms |
1017364 KB |
Output is correct |
54 |
Correct |
195 ms |
1017424 KB |
Output is correct |
55 |
Correct |
185 ms |
1017420 KB |
Output is correct |
56 |
Correct |
188 ms |
1017480 KB |
Output is correct |
57 |
Correct |
187 ms |
1017336 KB |
Output is correct |
58 |
Correct |
319 ms |
1017532 KB |
Output is correct |
59 |
Correct |
300 ms |
1017412 KB |
Output is correct |
60 |
Correct |
305 ms |
1017504 KB |
Output is correct |
61 |
Correct |
307 ms |
1017284 KB |
Output is correct |
62 |
Correct |
205 ms |
1017388 KB |
Output is correct |
63 |
Correct |
187 ms |
1017388 KB |
Output is correct |
64 |
Correct |
208 ms |
1017428 KB |
Output is correct |
65 |
Correct |
232 ms |
1017428 KB |
Output is correct |
66 |
Correct |
319 ms |
1017524 KB |
Output is correct |
67 |
Correct |
295 ms |
1017484 KB |
Output is correct |
68 |
Correct |
306 ms |
1017532 KB |
Output is correct |
69 |
Correct |
306 ms |
1017524 KB |
Output is correct |
70 |
Correct |
307 ms |
1017528 KB |
Output is correct |
71 |
Correct |
300 ms |
1017436 KB |
Output is correct |
72 |
Correct |
200 ms |
1017324 KB |
Output is correct |
73 |
Correct |
186 ms |
1017428 KB |
Output is correct |