#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define cd complex<double>
#define p_q priority_queue
#define vi vector<int>
#define vvi vector<vi>
#define vvvi vector<vvi>
#define vvvvi vector<vvvi>
using namespace std;
const int inf=1e9+7;
int main(){
AquA;
int n;
cin >> n;
string s;
cin >> s;
int cr=0,cb=0,cg=0;
vi rr,bb,gg,tg(n+1),tb(n+1),tr(n+1);
rr.push_back(-1);
bb.push_back(-1);
gg.push_back(-1);
for(int i=0;i<n;i++){
tr[i]=tr[(i-1)*(i>0)];
tb[i]=tb[(i-1)*(i>0)];
tg[i]=tg[(i-1)*(i>0)];
if(s[i]=='R'){
tr[i]++;
cr++;
rr.push_back(i);
}
else if(s[i]=='Y'){
tb[i]++;
cb++;
bb.push_back(i);
}
else{
tg[i]++;
cg++;
gg.push_back(i);
}
}
vvvvi dp(cr+1,vvvi(cb+1,vvi(cg+1,vi(3,inf))));
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
for(int r=0;r<=cr;r++){
for(int b=0;b<=cb;b++){
for(int g=0;g<=cg;g++){
if(r>0){
int c=max(0,tg[rr[r]]-g)+max(0,tb[rr[r]]-b);
dp[r][b][g][0]=min(dp[r][b][g][0],min(dp[r-1][b][g][1],dp[r-1][b][g][2])+c);
}
if(b>0){
int c=max(0,tg[bb[b]]-g)+max(0,tr[bb[b]]-r);
dp[r][b][g][1]=min(dp[r][b][g][1],min(dp[r][b-1][g][0],dp[r][b-1][g][2])+c);
}
if(g>0){
int c=max(0,tb[gg[g]]-b)+max(0,tr[gg[g]]-r);
dp[r][b][g][2]=min(dp[r][b][g][2],min(dp[r][b][g-1][0],dp[r][b][g-1][1])+c);
}
//cout << r << " " << b << " " << g << " " << dp[r][b][g][0] << " " << dp[r][b][g][1] << " " << dp[r][b][g][2] << "\n";
}
}
}
int h=min(dp[cr][cb][cg][0],min(dp[cr][cb][cg][1],dp[cr][cb][cg][2]));
if(h==inf){
cout << -1 << "\n";
}
else{
cout << h << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
308 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
308 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
844 KB |
Output is correct |
19 |
Correct |
2 ms |
844 KB |
Output is correct |
20 |
Correct |
1 ms |
716 KB |
Output is correct |
21 |
Correct |
1 ms |
844 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
1 ms |
716 KB |
Output is correct |
25 |
Correct |
1 ms |
396 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
1 ms |
844 KB |
Output is correct |
29 |
Correct |
1 ms |
844 KB |
Output is correct |
30 |
Correct |
1 ms |
844 KB |
Output is correct |
31 |
Correct |
1 ms |
844 KB |
Output is correct |
32 |
Correct |
1 ms |
844 KB |
Output is correct |
33 |
Correct |
1 ms |
480 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
696 KB |
Output is correct |
36 |
Correct |
1 ms |
716 KB |
Output is correct |
37 |
Correct |
1 ms |
716 KB |
Output is correct |
38 |
Correct |
1 ms |
700 KB |
Output is correct |
39 |
Correct |
1 ms |
844 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
4 ms |
2508 KB |
Output is correct |
3 |
Correct |
3 ms |
2508 KB |
Output is correct |
4 |
Correct |
4 ms |
2492 KB |
Output is correct |
5 |
Correct |
3 ms |
2508 KB |
Output is correct |
6 |
Correct |
5 ms |
2488 KB |
Output is correct |
7 |
Correct |
3 ms |
2492 KB |
Output is correct |
8 |
Correct |
3 ms |
2492 KB |
Output is correct |
9 |
Correct |
4 ms |
2508 KB |
Output is correct |
10 |
Correct |
3 ms |
2508 KB |
Output is correct |
11 |
Correct |
4 ms |
2508 KB |
Output is correct |
12 |
Correct |
1 ms |
844 KB |
Output is correct |
13 |
Correct |
2 ms |
1356 KB |
Output is correct |
14 |
Correct |
2 ms |
1740 KB |
Output is correct |
15 |
Correct |
3 ms |
2508 KB |
Output is correct |
16 |
Correct |
3 ms |
2508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
0 ms |
308 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
844 KB |
Output is correct |
19 |
Correct |
2 ms |
844 KB |
Output is correct |
20 |
Correct |
1 ms |
716 KB |
Output is correct |
21 |
Correct |
1 ms |
844 KB |
Output is correct |
22 |
Correct |
1 ms |
716 KB |
Output is correct |
23 |
Correct |
1 ms |
844 KB |
Output is correct |
24 |
Correct |
1 ms |
716 KB |
Output is correct |
25 |
Correct |
1 ms |
396 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
1 ms |
844 KB |
Output is correct |
29 |
Correct |
1 ms |
844 KB |
Output is correct |
30 |
Correct |
1 ms |
844 KB |
Output is correct |
31 |
Correct |
1 ms |
844 KB |
Output is correct |
32 |
Correct |
1 ms |
844 KB |
Output is correct |
33 |
Correct |
1 ms |
480 KB |
Output is correct |
34 |
Correct |
1 ms |
460 KB |
Output is correct |
35 |
Correct |
1 ms |
696 KB |
Output is correct |
36 |
Correct |
1 ms |
716 KB |
Output is correct |
37 |
Correct |
1 ms |
716 KB |
Output is correct |
38 |
Correct |
1 ms |
700 KB |
Output is correct |
39 |
Correct |
1 ms |
844 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
444 KB |
Output is correct |
42 |
Correct |
1 ms |
204 KB |
Output is correct |
43 |
Correct |
4 ms |
2508 KB |
Output is correct |
44 |
Correct |
3 ms |
2508 KB |
Output is correct |
45 |
Correct |
4 ms |
2492 KB |
Output is correct |
46 |
Correct |
3 ms |
2508 KB |
Output is correct |
47 |
Correct |
5 ms |
2488 KB |
Output is correct |
48 |
Correct |
3 ms |
2492 KB |
Output is correct |
49 |
Correct |
3 ms |
2492 KB |
Output is correct |
50 |
Correct |
4 ms |
2508 KB |
Output is correct |
51 |
Correct |
3 ms |
2508 KB |
Output is correct |
52 |
Correct |
4 ms |
2508 KB |
Output is correct |
53 |
Correct |
1 ms |
844 KB |
Output is correct |
54 |
Correct |
2 ms |
1356 KB |
Output is correct |
55 |
Correct |
2 ms |
1740 KB |
Output is correct |
56 |
Correct |
3 ms |
2508 KB |
Output is correct |
57 |
Correct |
3 ms |
2508 KB |
Output is correct |
58 |
Correct |
182 ms |
133368 KB |
Output is correct |
59 |
Correct |
175 ms |
133148 KB |
Output is correct |
60 |
Correct |
180 ms |
132348 KB |
Output is correct |
61 |
Correct |
176 ms |
133824 KB |
Output is correct |
62 |
Correct |
15 ms |
11084 KB |
Output is correct |
63 |
Correct |
26 ms |
19628 KB |
Output is correct |
64 |
Correct |
87 ms |
65828 KB |
Output is correct |
65 |
Correct |
124 ms |
94932 KB |
Output is correct |
66 |
Correct |
174 ms |
131616 KB |
Output is correct |
67 |
Correct |
172 ms |
131608 KB |
Output is correct |
68 |
Correct |
175 ms |
133572 KB |
Output is correct |
69 |
Correct |
185 ms |
134800 KB |
Output is correct |
70 |
Correct |
177 ms |
134216 KB |
Output is correct |
71 |
Correct |
174 ms |
131060 KB |
Output is correct |
72 |
Correct |
47 ms |
34628 KB |
Output is correct |
73 |
Correct |
14 ms |
10572 KB |
Output is correct |