#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
1042196 KB |
Output is correct |
2 |
Correct |
145 ms |
1042324 KB |
Output is correct |
3 |
Correct |
160 ms |
1042092 KB |
Output is correct |
4 |
Correct |
144 ms |
1042168 KB |
Output is correct |
5 |
Correct |
147 ms |
1042236 KB |
Output is correct |
6 |
Correct |
146 ms |
1042328 KB |
Output is correct |
7 |
Correct |
151 ms |
1042312 KB |
Output is correct |
8 |
Correct |
153 ms |
1042260 KB |
Output is correct |
9 |
Correct |
147 ms |
1042264 KB |
Output is correct |
10 |
Correct |
148 ms |
1042256 KB |
Output is correct |
11 |
Correct |
146 ms |
1042260 KB |
Output is correct |
12 |
Correct |
148 ms |
1042256 KB |
Output is correct |
13 |
Correct |
146 ms |
1042284 KB |
Output is correct |
14 |
Correct |
149 ms |
1042260 KB |
Output is correct |
15 |
Correct |
150 ms |
1042368 KB |
Output is correct |
16 |
Correct |
147 ms |
1042260 KB |
Output is correct |
17 |
Correct |
149 ms |
1042156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
1042196 KB |
Output is correct |
2 |
Correct |
145 ms |
1042324 KB |
Output is correct |
3 |
Correct |
160 ms |
1042092 KB |
Output is correct |
4 |
Correct |
144 ms |
1042168 KB |
Output is correct |
5 |
Correct |
147 ms |
1042236 KB |
Output is correct |
6 |
Correct |
146 ms |
1042328 KB |
Output is correct |
7 |
Correct |
151 ms |
1042312 KB |
Output is correct |
8 |
Correct |
153 ms |
1042260 KB |
Output is correct |
9 |
Correct |
147 ms |
1042264 KB |
Output is correct |
10 |
Correct |
148 ms |
1042256 KB |
Output is correct |
11 |
Correct |
146 ms |
1042260 KB |
Output is correct |
12 |
Correct |
148 ms |
1042256 KB |
Output is correct |
13 |
Correct |
146 ms |
1042284 KB |
Output is correct |
14 |
Correct |
149 ms |
1042260 KB |
Output is correct |
15 |
Correct |
150 ms |
1042368 KB |
Output is correct |
16 |
Correct |
147 ms |
1042260 KB |
Output is correct |
17 |
Correct |
149 ms |
1042156 KB |
Output is correct |
18 |
Correct |
147 ms |
1042516 KB |
Output is correct |
19 |
Correct |
149 ms |
1042652 KB |
Output is correct |
20 |
Correct |
149 ms |
1042476 KB |
Output is correct |
21 |
Correct |
153 ms |
1042648 KB |
Output is correct |
22 |
Correct |
157 ms |
1042544 KB |
Output is correct |
23 |
Correct |
157 ms |
1042628 KB |
Output is correct |
24 |
Correct |
156 ms |
1042512 KB |
Output is correct |
25 |
Correct |
147 ms |
1042484 KB |
Output is correct |
26 |
Correct |
146 ms |
1042512 KB |
Output is correct |
27 |
Correct |
150 ms |
1042632 KB |
Output is correct |
28 |
Correct |
147 ms |
1042448 KB |
Output is correct |
29 |
Correct |
161 ms |
1042624 KB |
Output is correct |
30 |
Correct |
151 ms |
1042464 KB |
Output is correct |
31 |
Correct |
167 ms |
1042636 KB |
Output is correct |
32 |
Correct |
147 ms |
1042512 KB |
Output is correct |
33 |
Correct |
151 ms |
1042476 KB |
Output is correct |
34 |
Correct |
149 ms |
1042516 KB |
Output is correct |
35 |
Correct |
155 ms |
1042648 KB |
Output is correct |
36 |
Correct |
149 ms |
1042512 KB |
Output is correct |
37 |
Correct |
153 ms |
1042524 KB |
Output is correct |
38 |
Correct |
152 ms |
1042852 KB |
Output is correct |
39 |
Correct |
148 ms |
1042544 KB |
Output is correct |
40 |
Correct |
151 ms |
1042512 KB |
Output is correct |
41 |
Correct |
150 ms |
1042508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
1042120 KB |
Output is correct |
2 |
Correct |
148 ms |
1044116 KB |
Output is correct |
3 |
Correct |
155 ms |
1044188 KB |
Output is correct |
4 |
Correct |
151 ms |
1044384 KB |
Output is correct |
5 |
Correct |
145 ms |
1044148 KB |
Output is correct |
6 |
Correct |
150 ms |
1044124 KB |
Output is correct |
7 |
Correct |
147 ms |
1044048 KB |
Output is correct |
8 |
Correct |
149 ms |
1044300 KB |
Output is correct |
9 |
Correct |
151 ms |
1044080 KB |
Output is correct |
10 |
Correct |
150 ms |
1044048 KB |
Output is correct |
11 |
Correct |
151 ms |
1044236 KB |
Output is correct |
12 |
Correct |
156 ms |
1043212 KB |
Output is correct |
13 |
Correct |
152 ms |
1043524 KB |
Output is correct |
14 |
Correct |
155 ms |
1043792 KB |
Output is correct |
15 |
Correct |
151 ms |
1044196 KB |
Output is correct |
16 |
Correct |
147 ms |
1044048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
1042196 KB |
Output is correct |
2 |
Correct |
145 ms |
1042324 KB |
Output is correct |
3 |
Correct |
160 ms |
1042092 KB |
Output is correct |
4 |
Correct |
144 ms |
1042168 KB |
Output is correct |
5 |
Correct |
147 ms |
1042236 KB |
Output is correct |
6 |
Correct |
146 ms |
1042328 KB |
Output is correct |
7 |
Correct |
151 ms |
1042312 KB |
Output is correct |
8 |
Correct |
153 ms |
1042260 KB |
Output is correct |
9 |
Correct |
147 ms |
1042264 KB |
Output is correct |
10 |
Correct |
148 ms |
1042256 KB |
Output is correct |
11 |
Correct |
146 ms |
1042260 KB |
Output is correct |
12 |
Correct |
148 ms |
1042256 KB |
Output is correct |
13 |
Correct |
146 ms |
1042284 KB |
Output is correct |
14 |
Correct |
149 ms |
1042260 KB |
Output is correct |
15 |
Correct |
150 ms |
1042368 KB |
Output is correct |
16 |
Correct |
147 ms |
1042260 KB |
Output is correct |
17 |
Correct |
149 ms |
1042156 KB |
Output is correct |
18 |
Correct |
147 ms |
1042516 KB |
Output is correct |
19 |
Correct |
149 ms |
1042652 KB |
Output is correct |
20 |
Correct |
149 ms |
1042476 KB |
Output is correct |
21 |
Correct |
153 ms |
1042648 KB |
Output is correct |
22 |
Correct |
157 ms |
1042544 KB |
Output is correct |
23 |
Correct |
157 ms |
1042628 KB |
Output is correct |
24 |
Correct |
156 ms |
1042512 KB |
Output is correct |
25 |
Correct |
147 ms |
1042484 KB |
Output is correct |
26 |
Correct |
146 ms |
1042512 KB |
Output is correct |
27 |
Correct |
150 ms |
1042632 KB |
Output is correct |
28 |
Correct |
147 ms |
1042448 KB |
Output is correct |
29 |
Correct |
161 ms |
1042624 KB |
Output is correct |
30 |
Correct |
151 ms |
1042464 KB |
Output is correct |
31 |
Correct |
167 ms |
1042636 KB |
Output is correct |
32 |
Correct |
147 ms |
1042512 KB |
Output is correct |
33 |
Correct |
151 ms |
1042476 KB |
Output is correct |
34 |
Correct |
149 ms |
1042516 KB |
Output is correct |
35 |
Correct |
155 ms |
1042648 KB |
Output is correct |
36 |
Correct |
149 ms |
1042512 KB |
Output is correct |
37 |
Correct |
153 ms |
1042524 KB |
Output is correct |
38 |
Correct |
152 ms |
1042852 KB |
Output is correct |
39 |
Correct |
148 ms |
1042544 KB |
Output is correct |
40 |
Correct |
151 ms |
1042512 KB |
Output is correct |
41 |
Correct |
150 ms |
1042508 KB |
Output is correct |
42 |
Correct |
146 ms |
1042120 KB |
Output is correct |
43 |
Correct |
148 ms |
1044116 KB |
Output is correct |
44 |
Correct |
155 ms |
1044188 KB |
Output is correct |
45 |
Correct |
151 ms |
1044384 KB |
Output is correct |
46 |
Correct |
145 ms |
1044148 KB |
Output is correct |
47 |
Correct |
150 ms |
1044124 KB |
Output is correct |
48 |
Correct |
147 ms |
1044048 KB |
Output is correct |
49 |
Correct |
149 ms |
1044300 KB |
Output is correct |
50 |
Correct |
151 ms |
1044080 KB |
Output is correct |
51 |
Correct |
150 ms |
1044048 KB |
Output is correct |
52 |
Correct |
151 ms |
1044236 KB |
Output is correct |
53 |
Correct |
156 ms |
1043212 KB |
Output is correct |
54 |
Correct |
152 ms |
1043524 KB |
Output is correct |
55 |
Correct |
155 ms |
1043792 KB |
Output is correct |
56 |
Correct |
151 ms |
1044196 KB |
Output is correct |
57 |
Correct |
147 ms |
1044048 KB |
Output is correct |
58 |
Correct |
220 ms |
1044568 KB |
Output is correct |
59 |
Correct |
221 ms |
1044812 KB |
Output is correct |
60 |
Correct |
219 ms |
1044700 KB |
Output is correct |
61 |
Correct |
232 ms |
1044820 KB |
Output is correct |
62 |
Correct |
148 ms |
1044560 KB |
Output is correct |
63 |
Correct |
156 ms |
1044552 KB |
Output is correct |
64 |
Correct |
169 ms |
1044556 KB |
Output is correct |
65 |
Correct |
180 ms |
1044820 KB |
Output is correct |
66 |
Correct |
226 ms |
1044576 KB |
Output is correct |
67 |
Correct |
222 ms |
1044692 KB |
Output is correct |
68 |
Correct |
235 ms |
1044816 KB |
Output is correct |
69 |
Correct |
222 ms |
1044516 KB |
Output is correct |
70 |
Correct |
220 ms |
1044564 KB |
Output is correct |
71 |
Correct |
219 ms |
1044556 KB |
Output is correct |
72 |
Correct |
150 ms |
1044564 KB |
Output is correct |
73 |
Correct |
154 ms |
1043556 KB |
Output is correct |