#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];
}
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 |
383 ms |
757308 KB |
Output is correct |
2 |
Correct |
246 ms |
757432 KB |
Output is correct |
3 |
Correct |
257 ms |
757412 KB |
Output is correct |
4 |
Correct |
250 ms |
757356 KB |
Output is correct |
5 |
Correct |
251 ms |
757480 KB |
Output is correct |
6 |
Correct |
251 ms |
757368 KB |
Output is correct |
7 |
Correct |
255 ms |
757360 KB |
Output is correct |
8 |
Correct |
253 ms |
757452 KB |
Output is correct |
9 |
Correct |
253 ms |
757420 KB |
Output is correct |
10 |
Correct |
248 ms |
757440 KB |
Output is correct |
11 |
Correct |
255 ms |
757376 KB |
Output is correct |
12 |
Correct |
249 ms |
757468 KB |
Output is correct |
13 |
Correct |
253 ms |
757396 KB |
Output is correct |
14 |
Correct |
251 ms |
757400 KB |
Output is correct |
15 |
Correct |
248 ms |
757480 KB |
Output is correct |
16 |
Correct |
254 ms |
757380 KB |
Output is correct |
17 |
Correct |
251 ms |
757464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
383 ms |
757308 KB |
Output is correct |
2 |
Correct |
246 ms |
757432 KB |
Output is correct |
3 |
Correct |
257 ms |
757412 KB |
Output is correct |
4 |
Correct |
250 ms |
757356 KB |
Output is correct |
5 |
Correct |
251 ms |
757480 KB |
Output is correct |
6 |
Correct |
251 ms |
757368 KB |
Output is correct |
7 |
Correct |
255 ms |
757360 KB |
Output is correct |
8 |
Correct |
253 ms |
757452 KB |
Output is correct |
9 |
Correct |
253 ms |
757420 KB |
Output is correct |
10 |
Correct |
248 ms |
757440 KB |
Output is correct |
11 |
Correct |
255 ms |
757376 KB |
Output is correct |
12 |
Correct |
249 ms |
757468 KB |
Output is correct |
13 |
Correct |
253 ms |
757396 KB |
Output is correct |
14 |
Correct |
251 ms |
757400 KB |
Output is correct |
15 |
Correct |
248 ms |
757480 KB |
Output is correct |
16 |
Correct |
254 ms |
757380 KB |
Output is correct |
17 |
Correct |
251 ms |
757464 KB |
Output is correct |
18 |
Correct |
256 ms |
757580 KB |
Output is correct |
19 |
Correct |
253 ms |
757628 KB |
Output is correct |
20 |
Correct |
250 ms |
757588 KB |
Output is correct |
21 |
Correct |
258 ms |
757616 KB |
Output is correct |
22 |
Correct |
262 ms |
757572 KB |
Output is correct |
23 |
Correct |
255 ms |
757604 KB |
Output is correct |
24 |
Correct |
258 ms |
757752 KB |
Output is correct |
25 |
Correct |
250 ms |
757572 KB |
Output is correct |
26 |
Correct |
253 ms |
757516 KB |
Output is correct |
27 |
Correct |
253 ms |
757612 KB |
Output is correct |
28 |
Correct |
249 ms |
757532 KB |
Output is correct |
29 |
Correct |
250 ms |
757580 KB |
Output is correct |
30 |
Correct |
252 ms |
757580 KB |
Output is correct |
31 |
Correct |
255 ms |
757576 KB |
Output is correct |
32 |
Correct |
254 ms |
757500 KB |
Output is correct |
33 |
Correct |
260 ms |
757500 KB |
Output is correct |
34 |
Correct |
256 ms |
757600 KB |
Output is correct |
35 |
Correct |
254 ms |
757572 KB |
Output is correct |
36 |
Correct |
251 ms |
757512 KB |
Output is correct |
37 |
Correct |
254 ms |
757560 KB |
Output is correct |
38 |
Correct |
255 ms |
757616 KB |
Output is correct |
39 |
Correct |
250 ms |
757552 KB |
Output is correct |
40 |
Correct |
259 ms |
757412 KB |
Output is correct |
41 |
Correct |
256 ms |
757584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
256 ms |
757404 KB |
Output is correct |
2 |
Correct |
251 ms |
757964 KB |
Output is correct |
3 |
Correct |
253 ms |
758080 KB |
Output is correct |
4 |
Correct |
248 ms |
758088 KB |
Output is correct |
5 |
Correct |
255 ms |
758236 KB |
Output is correct |
6 |
Correct |
252 ms |
758068 KB |
Output is correct |
7 |
Correct |
248 ms |
758060 KB |
Output is correct |
8 |
Correct |
256 ms |
758092 KB |
Output is correct |
9 |
Correct |
237 ms |
758028 KB |
Output is correct |
10 |
Correct |
234 ms |
758092 KB |
Output is correct |
11 |
Correct |
233 ms |
758028 KB |
Output is correct |
12 |
Correct |
233 ms |
757772 KB |
Output is correct |
13 |
Correct |
236 ms |
757876 KB |
Output is correct |
14 |
Correct |
239 ms |
757928 KB |
Output is correct |
15 |
Correct |
234 ms |
757972 KB |
Output is correct |
16 |
Correct |
233 ms |
757988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
383 ms |
757308 KB |
Output is correct |
2 |
Correct |
246 ms |
757432 KB |
Output is correct |
3 |
Correct |
257 ms |
757412 KB |
Output is correct |
4 |
Correct |
250 ms |
757356 KB |
Output is correct |
5 |
Correct |
251 ms |
757480 KB |
Output is correct |
6 |
Correct |
251 ms |
757368 KB |
Output is correct |
7 |
Correct |
255 ms |
757360 KB |
Output is correct |
8 |
Correct |
253 ms |
757452 KB |
Output is correct |
9 |
Correct |
253 ms |
757420 KB |
Output is correct |
10 |
Correct |
248 ms |
757440 KB |
Output is correct |
11 |
Correct |
255 ms |
757376 KB |
Output is correct |
12 |
Correct |
249 ms |
757468 KB |
Output is correct |
13 |
Correct |
253 ms |
757396 KB |
Output is correct |
14 |
Correct |
251 ms |
757400 KB |
Output is correct |
15 |
Correct |
248 ms |
757480 KB |
Output is correct |
16 |
Correct |
254 ms |
757380 KB |
Output is correct |
17 |
Correct |
251 ms |
757464 KB |
Output is correct |
18 |
Correct |
256 ms |
757580 KB |
Output is correct |
19 |
Correct |
253 ms |
757628 KB |
Output is correct |
20 |
Correct |
250 ms |
757588 KB |
Output is correct |
21 |
Correct |
258 ms |
757616 KB |
Output is correct |
22 |
Correct |
262 ms |
757572 KB |
Output is correct |
23 |
Correct |
255 ms |
757604 KB |
Output is correct |
24 |
Correct |
258 ms |
757752 KB |
Output is correct |
25 |
Correct |
250 ms |
757572 KB |
Output is correct |
26 |
Correct |
253 ms |
757516 KB |
Output is correct |
27 |
Correct |
253 ms |
757612 KB |
Output is correct |
28 |
Correct |
249 ms |
757532 KB |
Output is correct |
29 |
Correct |
250 ms |
757580 KB |
Output is correct |
30 |
Correct |
252 ms |
757580 KB |
Output is correct |
31 |
Correct |
255 ms |
757576 KB |
Output is correct |
32 |
Correct |
254 ms |
757500 KB |
Output is correct |
33 |
Correct |
260 ms |
757500 KB |
Output is correct |
34 |
Correct |
256 ms |
757600 KB |
Output is correct |
35 |
Correct |
254 ms |
757572 KB |
Output is correct |
36 |
Correct |
251 ms |
757512 KB |
Output is correct |
37 |
Correct |
254 ms |
757560 KB |
Output is correct |
38 |
Correct |
255 ms |
757616 KB |
Output is correct |
39 |
Correct |
250 ms |
757552 KB |
Output is correct |
40 |
Correct |
259 ms |
757412 KB |
Output is correct |
41 |
Correct |
256 ms |
757584 KB |
Output is correct |
42 |
Correct |
256 ms |
757404 KB |
Output is correct |
43 |
Correct |
251 ms |
757964 KB |
Output is correct |
44 |
Correct |
253 ms |
758080 KB |
Output is correct |
45 |
Correct |
248 ms |
758088 KB |
Output is correct |
46 |
Correct |
255 ms |
758236 KB |
Output is correct |
47 |
Correct |
252 ms |
758068 KB |
Output is correct |
48 |
Correct |
248 ms |
758060 KB |
Output is correct |
49 |
Correct |
256 ms |
758092 KB |
Output is correct |
50 |
Correct |
237 ms |
758028 KB |
Output is correct |
51 |
Correct |
234 ms |
758092 KB |
Output is correct |
52 |
Correct |
233 ms |
758028 KB |
Output is correct |
53 |
Correct |
233 ms |
757772 KB |
Output is correct |
54 |
Correct |
236 ms |
757876 KB |
Output is correct |
55 |
Correct |
239 ms |
757928 KB |
Output is correct |
56 |
Correct |
234 ms |
757972 KB |
Output is correct |
57 |
Correct |
233 ms |
757988 KB |
Output is correct |
58 |
Correct |
326 ms |
758908 KB |
Output is correct |
59 |
Correct |
323 ms |
758736 KB |
Output is correct |
60 |
Correct |
319 ms |
758820 KB |
Output is correct |
61 |
Correct |
329 ms |
758640 KB |
Output is correct |
62 |
Correct |
239 ms |
758672 KB |
Output is correct |
63 |
Correct |
239 ms |
758672 KB |
Output is correct |
64 |
Correct |
247 ms |
758652 KB |
Output is correct |
65 |
Correct |
262 ms |
758604 KB |
Output is correct |
66 |
Correct |
326 ms |
758740 KB |
Output is correct |
67 |
Correct |
313 ms |
758720 KB |
Output is correct |
68 |
Correct |
334 ms |
758604 KB |
Output is correct |
69 |
Correct |
320 ms |
758604 KB |
Output is correct |
70 |
Correct |
313 ms |
758712 KB |
Output is correct |
71 |
Correct |
310 ms |
758656 KB |
Output is correct |
72 |
Correct |
237 ms |
758652 KB |
Output is correct |
73 |
Correct |
237 ms |
758804 KB |
Output is correct |