#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define OYY 1000000005
#define mod 998244353
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mid (start+end)/2
#define lim 405
#define fi first
#define se second
int n;// 1 2 3
vector<int> v[4];
int dp[lim][lim][lim][4];
int gec[4][lim][4][lim];// renk index diğer index
inline int dpp(int red,int gre,int yel,int sira,int renk){
if(sira>=n){
return 0;
}
if(~dp[red][gre][yel][renk])return dp[red][gre][yel][renk];
int yum=OYY;
if(renk!=1 && red<(int)v[1].size()){
int deg=0,deg2=0;
if(gre>0)deg=gec[1][red][2][gre-1];
if(yel>0)deg2=gec[1][red][3][yel-1];
yum=min(yum,dpp(red+1,gre,yel,sira+1,1)+v[1][red]+deg+deg2-sira);
}
if(renk!=2 && gre<(int)v[2].size()){
int deg=0,deg2=0;
if(red>0)deg=gec[2][gre][1][red-1];
if(yel>0)deg2=gec[2][gre][3][yel-1];
yum=min(yum,dpp(red,gre+1,yel,sira+1,2)+v[2][gre]+deg+deg2-sira);
}
if(renk!=3 && yel<(int)v[3].size()){
int deg=0,deg2=0;
if(red>0)deg=gec[3][yel][1][red-1];
if(gre>0)deg2=gec[3][yel][2][gre-1];
yum=min(yum,dpp(red,gre,yel+1,sira+1,3)+v[3][yel]+deg+deg2-sira);
}
return dp[red][gre][yel][renk]=yum;
}
int main(){
faster
memset(dp,-1,sizeof(dp));
cin>>n;
string s;cin>>s;
for(int i=0;i<n;i++){
if(s[i]=='R')v[1].push_back(i);
else if(s[i]=='G')v[2].push_back(i);
else v[3].push_back(i);
}
for(int i=0;i<(int)v[1].size();i++){
int tut=0;
for(int j=0;j<(int)v[2].size();j++){
if(v[1][i]<v[2][j])tut++;
gec[1][i][2][j]=tut;
}
}
for(int i=0;i<(int)v[1].size();i++){
int tut=0;
for(int j=0;j<(int)v[3].size();j++){
if(v[1][i]<v[3][j])tut++;
gec[1][i][3][j]=tut;
}
}
for(int i=0;i<(int)v[2].size();i++){
int tut=0;
for(int j=0;j<(int)v[1].size();j++){
if(v[2][i]<v[1][j])tut++;
gec[2][i][1][j]=tut;
}
}
for(int i=0;i<(int)v[2].size();i++){
int tut=0;
for(int j=0;j<(int)v[3].size();j++){
if(v[2][i]<v[3][j])tut++;
gec[2][i][3][j]=tut;
}
}
for(int i=0;i<(int)v[3].size();i++){
int tut=0;
for(int j=0;j<(int)v[1].size();j++){
if(v[3][i]<v[1][j])tut++;
gec[3][i][1][j]=tut;
}
}
for(int i=0;i<(int)v[3].size();i++){
int tut=0;
for(int j=0;j<(int)v[2].size();j++){
if(v[3][i]<v[2][j])tut++;
gec[3][i][2][j]=tut;
}
}
string h="";
int deg=dpp(0,0,0,0,0);
if(deg==OYY)deg=-1;
cout<<deg<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
1042288 KB |
Output is correct |
2 |
Correct |
145 ms |
1042340 KB |
Output is correct |
3 |
Correct |
144 ms |
1042256 KB |
Output is correct |
4 |
Correct |
144 ms |
1042292 KB |
Output is correct |
5 |
Correct |
147 ms |
1042256 KB |
Output is correct |
6 |
Correct |
150 ms |
1042236 KB |
Output is correct |
7 |
Correct |
146 ms |
1042432 KB |
Output is correct |
8 |
Correct |
146 ms |
1042364 KB |
Output is correct |
9 |
Correct |
148 ms |
1042212 KB |
Output is correct |
10 |
Correct |
146 ms |
1042220 KB |
Output is correct |
11 |
Correct |
147 ms |
1042260 KB |
Output is correct |
12 |
Correct |
146 ms |
1042316 KB |
Output is correct |
13 |
Correct |
146 ms |
1042228 KB |
Output is correct |
14 |
Correct |
143 ms |
1042292 KB |
Output is correct |
15 |
Correct |
155 ms |
1042428 KB |
Output is correct |
16 |
Correct |
146 ms |
1042260 KB |
Output is correct |
17 |
Correct |
158 ms |
1042248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
1042288 KB |
Output is correct |
2 |
Correct |
145 ms |
1042340 KB |
Output is correct |
3 |
Correct |
144 ms |
1042256 KB |
Output is correct |
4 |
Correct |
144 ms |
1042292 KB |
Output is correct |
5 |
Correct |
147 ms |
1042256 KB |
Output is correct |
6 |
Correct |
150 ms |
1042236 KB |
Output is correct |
7 |
Correct |
146 ms |
1042432 KB |
Output is correct |
8 |
Correct |
146 ms |
1042364 KB |
Output is correct |
9 |
Correct |
148 ms |
1042212 KB |
Output is correct |
10 |
Correct |
146 ms |
1042220 KB |
Output is correct |
11 |
Correct |
147 ms |
1042260 KB |
Output is correct |
12 |
Correct |
146 ms |
1042316 KB |
Output is correct |
13 |
Correct |
146 ms |
1042228 KB |
Output is correct |
14 |
Correct |
143 ms |
1042292 KB |
Output is correct |
15 |
Correct |
155 ms |
1042428 KB |
Output is correct |
16 |
Correct |
146 ms |
1042260 KB |
Output is correct |
17 |
Correct |
158 ms |
1042248 KB |
Output is correct |
18 |
Correct |
143 ms |
1042524 KB |
Output is correct |
19 |
Correct |
142 ms |
1042656 KB |
Output is correct |
20 |
Correct |
149 ms |
1042556 KB |
Output is correct |
21 |
Correct |
151 ms |
1042508 KB |
Output is correct |
22 |
Correct |
152 ms |
1042676 KB |
Output is correct |
23 |
Correct |
145 ms |
1042684 KB |
Output is correct |
24 |
Correct |
145 ms |
1042512 KB |
Output is correct |
25 |
Correct |
151 ms |
1042852 KB |
Output is correct |
26 |
Correct |
156 ms |
1042588 KB |
Output is correct |
27 |
Correct |
146 ms |
1042652 KB |
Output is correct |
28 |
Correct |
147 ms |
1042888 KB |
Output is correct |
29 |
Correct |
143 ms |
1042516 KB |
Output is correct |
30 |
Correct |
145 ms |
1042512 KB |
Output is correct |
31 |
Correct |
145 ms |
1042652 KB |
Output is correct |
32 |
Correct |
147 ms |
1042664 KB |
Output is correct |
33 |
Correct |
150 ms |
1042600 KB |
Output is correct |
34 |
Correct |
151 ms |
1042472 KB |
Output is correct |
35 |
Correct |
156 ms |
1042556 KB |
Output is correct |
36 |
Correct |
148 ms |
1042700 KB |
Output is correct |
37 |
Correct |
162 ms |
1042468 KB |
Output is correct |
38 |
Correct |
146 ms |
1042516 KB |
Output is correct |
39 |
Correct |
146 ms |
1042540 KB |
Output is correct |
40 |
Correct |
151 ms |
1042504 KB |
Output is correct |
41 |
Correct |
145 ms |
1042700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
145 ms |
1042220 KB |
Output is correct |
2 |
Correct |
159 ms |
1044372 KB |
Output is correct |
3 |
Correct |
150 ms |
1044196 KB |
Output is correct |
4 |
Correct |
147 ms |
1044048 KB |
Output is correct |
5 |
Correct |
162 ms |
1044056 KB |
Output is correct |
6 |
Correct |
146 ms |
1044300 KB |
Output is correct |
7 |
Correct |
148 ms |
1044100 KB |
Output is correct |
8 |
Correct |
146 ms |
1044052 KB |
Output is correct |
9 |
Correct |
152 ms |
1044088 KB |
Output is correct |
10 |
Correct |
147 ms |
1044256 KB |
Output is correct |
11 |
Correct |
150 ms |
1044132 KB |
Output is correct |
12 |
Correct |
153 ms |
1043176 KB |
Output is correct |
13 |
Correct |
152 ms |
1043560 KB |
Output is correct |
14 |
Correct |
147 ms |
1043664 KB |
Output is correct |
15 |
Correct |
151 ms |
1044244 KB |
Output is correct |
16 |
Correct |
153 ms |
1044052 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
1042288 KB |
Output is correct |
2 |
Correct |
145 ms |
1042340 KB |
Output is correct |
3 |
Correct |
144 ms |
1042256 KB |
Output is correct |
4 |
Correct |
144 ms |
1042292 KB |
Output is correct |
5 |
Correct |
147 ms |
1042256 KB |
Output is correct |
6 |
Correct |
150 ms |
1042236 KB |
Output is correct |
7 |
Correct |
146 ms |
1042432 KB |
Output is correct |
8 |
Correct |
146 ms |
1042364 KB |
Output is correct |
9 |
Correct |
148 ms |
1042212 KB |
Output is correct |
10 |
Correct |
146 ms |
1042220 KB |
Output is correct |
11 |
Correct |
147 ms |
1042260 KB |
Output is correct |
12 |
Correct |
146 ms |
1042316 KB |
Output is correct |
13 |
Correct |
146 ms |
1042228 KB |
Output is correct |
14 |
Correct |
143 ms |
1042292 KB |
Output is correct |
15 |
Correct |
155 ms |
1042428 KB |
Output is correct |
16 |
Correct |
146 ms |
1042260 KB |
Output is correct |
17 |
Correct |
158 ms |
1042248 KB |
Output is correct |
18 |
Correct |
143 ms |
1042524 KB |
Output is correct |
19 |
Correct |
142 ms |
1042656 KB |
Output is correct |
20 |
Correct |
149 ms |
1042556 KB |
Output is correct |
21 |
Correct |
151 ms |
1042508 KB |
Output is correct |
22 |
Correct |
152 ms |
1042676 KB |
Output is correct |
23 |
Correct |
145 ms |
1042684 KB |
Output is correct |
24 |
Correct |
145 ms |
1042512 KB |
Output is correct |
25 |
Correct |
151 ms |
1042852 KB |
Output is correct |
26 |
Correct |
156 ms |
1042588 KB |
Output is correct |
27 |
Correct |
146 ms |
1042652 KB |
Output is correct |
28 |
Correct |
147 ms |
1042888 KB |
Output is correct |
29 |
Correct |
143 ms |
1042516 KB |
Output is correct |
30 |
Correct |
145 ms |
1042512 KB |
Output is correct |
31 |
Correct |
145 ms |
1042652 KB |
Output is correct |
32 |
Correct |
147 ms |
1042664 KB |
Output is correct |
33 |
Correct |
150 ms |
1042600 KB |
Output is correct |
34 |
Correct |
151 ms |
1042472 KB |
Output is correct |
35 |
Correct |
156 ms |
1042556 KB |
Output is correct |
36 |
Correct |
148 ms |
1042700 KB |
Output is correct |
37 |
Correct |
162 ms |
1042468 KB |
Output is correct |
38 |
Correct |
146 ms |
1042516 KB |
Output is correct |
39 |
Correct |
146 ms |
1042540 KB |
Output is correct |
40 |
Correct |
151 ms |
1042504 KB |
Output is correct |
41 |
Correct |
145 ms |
1042700 KB |
Output is correct |
42 |
Correct |
145 ms |
1042220 KB |
Output is correct |
43 |
Correct |
159 ms |
1044372 KB |
Output is correct |
44 |
Correct |
150 ms |
1044196 KB |
Output is correct |
45 |
Correct |
147 ms |
1044048 KB |
Output is correct |
46 |
Correct |
162 ms |
1044056 KB |
Output is correct |
47 |
Correct |
146 ms |
1044300 KB |
Output is correct |
48 |
Correct |
148 ms |
1044100 KB |
Output is correct |
49 |
Correct |
146 ms |
1044052 KB |
Output is correct |
50 |
Correct |
152 ms |
1044088 KB |
Output is correct |
51 |
Correct |
147 ms |
1044256 KB |
Output is correct |
52 |
Correct |
150 ms |
1044132 KB |
Output is correct |
53 |
Correct |
153 ms |
1043176 KB |
Output is correct |
54 |
Correct |
152 ms |
1043560 KB |
Output is correct |
55 |
Correct |
147 ms |
1043664 KB |
Output is correct |
56 |
Correct |
151 ms |
1044244 KB |
Output is correct |
57 |
Correct |
153 ms |
1044052 KB |
Output is correct |
58 |
Correct |
230 ms |
1044584 KB |
Output is correct |
59 |
Correct |
223 ms |
1044780 KB |
Output is correct |
60 |
Correct |
229 ms |
1044560 KB |
Output is correct |
61 |
Correct |
226 ms |
1044600 KB |
Output is correct |
62 |
Correct |
154 ms |
1045112 KB |
Output is correct |
63 |
Correct |
151 ms |
1044572 KB |
Output is correct |
64 |
Correct |
160 ms |
1044688 KB |
Output is correct |
65 |
Correct |
175 ms |
1044820 KB |
Output is correct |
66 |
Correct |
237 ms |
1044560 KB |
Output is correct |
67 |
Correct |
215 ms |
1044696 KB |
Output is correct |
68 |
Correct |
251 ms |
1044628 KB |
Output is correct |
69 |
Correct |
224 ms |
1044692 KB |
Output is correct |
70 |
Correct |
240 ms |
1044632 KB |
Output is correct |
71 |
Correct |
215 ms |
1044564 KB |
Output is correct |
72 |
Correct |
151 ms |
1044548 KB |
Output is correct |
73 |
Correct |
150 ms |
1043536 KB |
Output is correct |