#include <bits/stdc++.h>
using namespace std;
typedef long long lo;
#define fi first
#define se second
#define endl "\n"
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)
const lo inf = 1000000000;
const lo li = 401;
const lo mod = 1000000007;
int n,m,a[li],k,flag,t,sr,sg,sy,arr[3][2][li][li],dp[li][li][li][3];
int cev;
string s;
vector<int> v[3];
inline int in(){
int x;
scanf("%d",&x);
return x;
}
inline int add(int x,int y,int q,int z){
if(z<0)return 0;
return arr[x][y][q][z];
}
inline int f(int r,int g,int y,int son,int sira){
int cevv=100000000;
if(sira==n)return 0;
if(~dp[r][g][y][son])return dp[r][g][y][son];
if((r==0 && g==0 && y==0 && sr) || (son!=0 && r<sr)){
cevv=min(cevv,f(r+1,g,y,0,sira+1)+v[0][r]+add(0,0,r,g-1)+add(0,1,r,y-1)-sira);
}
if(son!=1 && g<sg){
cevv=min(cevv,f(r,g+1,y,1,sira+1)+v[1][g]+add(1,0,g,r-1)+add(1,1,g,y-1)-sira);
}
if(son!=2 && y<sy){
cevv=min(cevv,f(r,g,y+1,2,sira+1)+v[2][y]+add(2,0,y,r-1)+add(2,1,y,g-1)-sira);
}
return dp[r][g][y][son]=cevv;
}
int main(void){
fio();
cin>>n>>s;
memset(dp,-1,sizeof(dp));
//R->G->Y
for(int i=0;i<n;i++){
if(s[i]=='R'){
v[0].pb(i);
sr++;
}
if(s[i]=='G'){
v[1].pb(i);
sg++;
}
if(s[i]=='Y'){
v[2].pb(i);
sy++;
}
}
for(int i=0;i<sr;i++){
int say=0;
for(int j=0;j<sg;j++){
if(v[0][i]<v[1][j])say++;
arr[0][0][i][j]=say;
}
say=0;
for(int j=0;j<sy;j++){
if(v[0][i]<v[2][j])say++;
arr[0][1][i][j]=say;
}
}
for(int i=0;i<sg;i++){
int say=0;
for(int j=0;j<sr;j++){
if(v[1][i]<v[0][j])say++;
arr[1][0][i][j]=say;
}
say=0;
for(int j=0;j<sy;j++){
if(v[1][i]<v[2][j])say++;
arr[1][1][i][j]=say;
}
}
for(int i=0;i<sy;i++){
int say=0;
for(int j=0;j<sr;j++){
if(v[2][i]<v[0][j])say++;
arr[2][0][i][j]=say;
}
say=0;
for(int j=0;j<sg;j++){
if(v[2][i]<v[1][j])say++;
arr[2][1][i][j]=say;
}
}
cev=f(0,0,0,0,0);
if(cev==100000000)cev=-1;
cout<<cev<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
437 ms |
757380 KB |
Output is correct |
2 |
Correct |
240 ms |
757580 KB |
Output is correct |
3 |
Correct |
234 ms |
757340 KB |
Output is correct |
4 |
Correct |
237 ms |
757376 KB |
Output is correct |
5 |
Correct |
233 ms |
757432 KB |
Output is correct |
6 |
Correct |
235 ms |
757428 KB |
Output is correct |
7 |
Correct |
234 ms |
757404 KB |
Output is correct |
8 |
Correct |
236 ms |
757452 KB |
Output is correct |
9 |
Correct |
248 ms |
757420 KB |
Output is correct |
10 |
Correct |
247 ms |
757432 KB |
Output is correct |
11 |
Correct |
236 ms |
757408 KB |
Output is correct |
12 |
Correct |
235 ms |
757412 KB |
Output is correct |
13 |
Correct |
240 ms |
757424 KB |
Output is correct |
14 |
Correct |
235 ms |
757444 KB |
Output is correct |
15 |
Correct |
232 ms |
757472 KB |
Output is correct |
16 |
Correct |
247 ms |
757396 KB |
Output is correct |
17 |
Correct |
235 ms |
757400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
437 ms |
757380 KB |
Output is correct |
2 |
Correct |
240 ms |
757580 KB |
Output is correct |
3 |
Correct |
234 ms |
757340 KB |
Output is correct |
4 |
Correct |
237 ms |
757376 KB |
Output is correct |
5 |
Correct |
233 ms |
757432 KB |
Output is correct |
6 |
Correct |
235 ms |
757428 KB |
Output is correct |
7 |
Correct |
234 ms |
757404 KB |
Output is correct |
8 |
Correct |
236 ms |
757452 KB |
Output is correct |
9 |
Correct |
248 ms |
757420 KB |
Output is correct |
10 |
Correct |
247 ms |
757432 KB |
Output is correct |
11 |
Correct |
236 ms |
757408 KB |
Output is correct |
12 |
Correct |
235 ms |
757412 KB |
Output is correct |
13 |
Correct |
240 ms |
757424 KB |
Output is correct |
14 |
Correct |
235 ms |
757444 KB |
Output is correct |
15 |
Correct |
232 ms |
757472 KB |
Output is correct |
16 |
Correct |
247 ms |
757396 KB |
Output is correct |
17 |
Correct |
235 ms |
757400 KB |
Output is correct |
18 |
Correct |
240 ms |
757580 KB |
Output is correct |
19 |
Correct |
237 ms |
757552 KB |
Output is correct |
20 |
Correct |
246 ms |
757576 KB |
Output is correct |
21 |
Correct |
254 ms |
757552 KB |
Output is correct |
22 |
Correct |
239 ms |
757700 KB |
Output is correct |
23 |
Correct |
233 ms |
757580 KB |
Output is correct |
24 |
Correct |
236 ms |
757580 KB |
Output is correct |
25 |
Correct |
237 ms |
757576 KB |
Output is correct |
26 |
Correct |
233 ms |
757580 KB |
Output is correct |
27 |
Correct |
235 ms |
757548 KB |
Output is correct |
28 |
Correct |
239 ms |
757672 KB |
Output is correct |
29 |
Correct |
238 ms |
757600 KB |
Output is correct |
30 |
Correct |
233 ms |
757548 KB |
Output is correct |
31 |
Correct |
234 ms |
757580 KB |
Output is correct |
32 |
Correct |
241 ms |
757540 KB |
Output is correct |
33 |
Correct |
237 ms |
757600 KB |
Output is correct |
34 |
Correct |
238 ms |
757596 KB |
Output is correct |
35 |
Correct |
237 ms |
757508 KB |
Output is correct |
36 |
Correct |
240 ms |
757588 KB |
Output is correct |
37 |
Correct |
238 ms |
757548 KB |
Output is correct |
38 |
Correct |
239 ms |
757612 KB |
Output is correct |
39 |
Correct |
244 ms |
757628 KB |
Output is correct |
40 |
Correct |
252 ms |
757528 KB |
Output is correct |
41 |
Correct |
244 ms |
757552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
243 ms |
757360 KB |
Output is correct |
2 |
Correct |
235 ms |
758004 KB |
Output is correct |
3 |
Correct |
236 ms |
758096 KB |
Output is correct |
4 |
Correct |
237 ms |
758256 KB |
Output is correct |
5 |
Correct |
238 ms |
758100 KB |
Output is correct |
6 |
Correct |
243 ms |
758072 KB |
Output is correct |
7 |
Correct |
241 ms |
758040 KB |
Output is correct |
8 |
Correct |
234 ms |
758040 KB |
Output is correct |
9 |
Correct |
236 ms |
757964 KB |
Output is correct |
10 |
Correct |
241 ms |
757996 KB |
Output is correct |
11 |
Correct |
253 ms |
758044 KB |
Output is correct |
12 |
Correct |
256 ms |
757704 KB |
Output is correct |
13 |
Correct |
240 ms |
757824 KB |
Output is correct |
14 |
Correct |
242 ms |
757908 KB |
Output is correct |
15 |
Correct |
238 ms |
758092 KB |
Output is correct |
16 |
Correct |
235 ms |
757980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
437 ms |
757380 KB |
Output is correct |
2 |
Correct |
240 ms |
757580 KB |
Output is correct |
3 |
Correct |
234 ms |
757340 KB |
Output is correct |
4 |
Correct |
237 ms |
757376 KB |
Output is correct |
5 |
Correct |
233 ms |
757432 KB |
Output is correct |
6 |
Correct |
235 ms |
757428 KB |
Output is correct |
7 |
Correct |
234 ms |
757404 KB |
Output is correct |
8 |
Correct |
236 ms |
757452 KB |
Output is correct |
9 |
Correct |
248 ms |
757420 KB |
Output is correct |
10 |
Correct |
247 ms |
757432 KB |
Output is correct |
11 |
Correct |
236 ms |
757408 KB |
Output is correct |
12 |
Correct |
235 ms |
757412 KB |
Output is correct |
13 |
Correct |
240 ms |
757424 KB |
Output is correct |
14 |
Correct |
235 ms |
757444 KB |
Output is correct |
15 |
Correct |
232 ms |
757472 KB |
Output is correct |
16 |
Correct |
247 ms |
757396 KB |
Output is correct |
17 |
Correct |
235 ms |
757400 KB |
Output is correct |
18 |
Correct |
240 ms |
757580 KB |
Output is correct |
19 |
Correct |
237 ms |
757552 KB |
Output is correct |
20 |
Correct |
246 ms |
757576 KB |
Output is correct |
21 |
Correct |
254 ms |
757552 KB |
Output is correct |
22 |
Correct |
239 ms |
757700 KB |
Output is correct |
23 |
Correct |
233 ms |
757580 KB |
Output is correct |
24 |
Correct |
236 ms |
757580 KB |
Output is correct |
25 |
Correct |
237 ms |
757576 KB |
Output is correct |
26 |
Correct |
233 ms |
757580 KB |
Output is correct |
27 |
Correct |
235 ms |
757548 KB |
Output is correct |
28 |
Correct |
239 ms |
757672 KB |
Output is correct |
29 |
Correct |
238 ms |
757600 KB |
Output is correct |
30 |
Correct |
233 ms |
757548 KB |
Output is correct |
31 |
Correct |
234 ms |
757580 KB |
Output is correct |
32 |
Correct |
241 ms |
757540 KB |
Output is correct |
33 |
Correct |
237 ms |
757600 KB |
Output is correct |
34 |
Correct |
238 ms |
757596 KB |
Output is correct |
35 |
Correct |
237 ms |
757508 KB |
Output is correct |
36 |
Correct |
240 ms |
757588 KB |
Output is correct |
37 |
Correct |
238 ms |
757548 KB |
Output is correct |
38 |
Correct |
239 ms |
757612 KB |
Output is correct |
39 |
Correct |
244 ms |
757628 KB |
Output is correct |
40 |
Correct |
252 ms |
757528 KB |
Output is correct |
41 |
Correct |
244 ms |
757552 KB |
Output is correct |
42 |
Correct |
243 ms |
757360 KB |
Output is correct |
43 |
Correct |
235 ms |
758004 KB |
Output is correct |
44 |
Correct |
236 ms |
758096 KB |
Output is correct |
45 |
Correct |
237 ms |
758256 KB |
Output is correct |
46 |
Correct |
238 ms |
758100 KB |
Output is correct |
47 |
Correct |
243 ms |
758072 KB |
Output is correct |
48 |
Correct |
241 ms |
758040 KB |
Output is correct |
49 |
Correct |
234 ms |
758040 KB |
Output is correct |
50 |
Correct |
236 ms |
757964 KB |
Output is correct |
51 |
Correct |
241 ms |
757996 KB |
Output is correct |
52 |
Correct |
253 ms |
758044 KB |
Output is correct |
53 |
Correct |
256 ms |
757704 KB |
Output is correct |
54 |
Correct |
240 ms |
757824 KB |
Output is correct |
55 |
Correct |
242 ms |
757908 KB |
Output is correct |
56 |
Correct |
238 ms |
758092 KB |
Output is correct |
57 |
Correct |
235 ms |
757980 KB |
Output is correct |
58 |
Correct |
321 ms |
758604 KB |
Output is correct |
59 |
Correct |
324 ms |
758736 KB |
Output is correct |
60 |
Correct |
331 ms |
758604 KB |
Output is correct |
61 |
Correct |
323 ms |
758656 KB |
Output is correct |
62 |
Correct |
237 ms |
758656 KB |
Output is correct |
63 |
Correct |
237 ms |
758672 KB |
Output is correct |
64 |
Correct |
253 ms |
758832 KB |
Output is correct |
65 |
Correct |
264 ms |
758684 KB |
Output is correct |
66 |
Correct |
328 ms |
758604 KB |
Output is correct |
67 |
Correct |
321 ms |
758604 KB |
Output is correct |
68 |
Correct |
327 ms |
758732 KB |
Output is correct |
69 |
Correct |
333 ms |
758612 KB |
Output is correct |
70 |
Correct |
335 ms |
758720 KB |
Output is correct |
71 |
Correct |
322 ms |
758784 KB |
Output is correct |
72 |
Correct |
241 ms |
758604 KB |
Output is correct |
73 |
Correct |
258 ms |
758664 KB |
Output is correct |