#include <bits/stdc++.h>
using namespace std;
const int N=405;
const int inf=1e9+7;
//int dp[3][N][N],tmp[3][N][N];
int pos[3][N],cnt[3];
char s[N];
void Clear(int a[3][N][N]){ for(int i=0;i<3;i++) for(int j=0;j<N;j++) for(int k=0;k<N;k++) a[i][j][k]=inf;}
void ckmn(int &a, int b){ a=min(a,b);}
int dp[3][N][N][N],val[3][N][N][N];
bool was[3][N][N][N];
int go[3][3][N][N];
int Get(int r, int g, int y, int f)
{
int ans=0;
if(f==0) ans=go[0][1][r][g]+go[0][2][r][y];
if(f==1) ans=go[1][0][g][r]+go[1][2][g][y];
if(f==2) ans=go[2][0][y][r]+go[2][1][y][g];
return ans;
/*int z;
if(f==0) z=pos[0][r];
if(f==1) z=pos[1][g];
if(f==2) z=pos[2][y];
int ans=0;
for(int i=0;i<3;i++) if(i!=f)
{
int p=i==0?r:i==1?g:y;
for(int j=cnt[i];j>p;j--) if(pos[i][j]<z) ans++;
}
return ans;*/
/*if(f==0)
{
if(g!=cnt[1]) val[f][r][g][y]=val[f][r][g+1][y]+(pos[0][r]>pos[1][g+1]);
else if(y!=cnt[2]) val[f][r][g][y]=val[f][r][g][y+1]+(pos[0][r]>pos[2][y+1]);
else val[f][r][g][y]=0;
}
if(f==1)
{
if(r!=cnt[0]) val[f][r][g][y]=val[f][r+1][g][y]+(pos[1][g]>pos[0][r+1]);
else if(y!=cnt[2]) val[f][r][g][y]=val[f][r][g][y+1]+(pos[1][g]>pos[2][y+1]);
else val[f][r][g][y]=0;
}
if(f==2)
{
if(g!=cnt[1]) val[f][r][g][y]=val[f][r][g+1][y]+(pos[2][y]>pos[1][g+1]);
else if(r!=cnt[0]) val[f][r][g][y]=val[f][r+1][g][y]+(pos[2][y]>pos[0][r+1]);
else val[f][r][g][y]=0;
}
return val[f][r][g][y];*/
}
int Solve(int r, int g, int y, int f)
{
if(was[f][r][g][y]) return dp[f][r][g][y];
was[f][r][g][y]=1;
dp[f][r][g][y]=inf;
int i=r+g+y;
if(i==0) return dp[f][r][g][y]=0;
if(f!=0 && r) ckmn(dp[f][r][g][y],Solve(r-1,g,y,0)+Get(r,g,y,0));
if(f!=1 && g) ckmn(dp[f][r][g][y],Solve(r,g-1,y,1)+Get(r,g,y,1));
if(f!=2 && y) ckmn(dp[f][r][g][y],Solve(r,g,y-1,2)+Get(r,g,y,2));
return dp[f][r][g][y];
}
int main()
{
int n;
scanf("%i",&n);
scanf("%s",s+1);
for(int i=1;i<=n;i++)
{
if(s[i]=='R') pos[0][++cnt[0]]=i;
if(s[i]=='G') pos[1][++cnt[1]]=i;
if(s[i]=='Y') pos[2][++cnt[2]]=i;
}
for(int q=0;q<3;q++) for(int w=0;w<3;w++) if(q!=w)
{
for(int i=1;i<=cnt[q];i++)
{
int now=0;
for(int j=cnt[w];j>=1;j--)
{
go[q][w][i][j]=now;
now+=pos[q][i]>pos[w][j];
}
go[q][w][i][0]=now;
}
}
//for(int i=0;i<3;i++) reverse(pos[i]+1,pos[i]+1+cnt[i]);
int ans=min(Solve(cnt[0],cnt[1],cnt[2],0),Solve(cnt[0],cnt[1],cnt[2],1));
printf("%i\n",ans==inf?-1:ans);
/*Clear(dp);
//printf("%i %i %i\n",cnt[0],cnt[1],cnt[2]);
if(cnt[0]) dp[0][cnt[0]-1][cnt[1]]=pos[0][cnt[0]]-1;
if(cnt[1]) dp[1][cnt[0]][cnt[1]-1]=pos[1][cnt[1]]-1;
if(cnt[2]) dp[2][cnt[0]][cnt[1]]=pos[2][cnt[2]]-1;
for(int i=2;i<=n;i++)
{
Clear(tmp);
for(int f=0;f<3;f++)
for(int r=0;r<=cnt[0];r++)
for(int g=0;g<=cnt[1];g++) if(dp[f][r][g]!=inf)
{
int y=n-i-r-g+1;
//printf("%i %i %i %i: %i\n",f,r,g,y,dp[f][r][g]);
if(f!=0 && r) ckmn(tmp[0][r-1][g],dp[f][r][g]+max(0,pos[0][r]-i));
if(f!=1 && g) ckmn(tmp[1][r][g-1],dp[f][r][g]+max(0,pos[1][g]-i));
if(f!=2 && y) ckmn(tmp[2][r][g],dp[f][r][g]+max(0,pos[2][y]-i));
}
for(int f=0;f<3;f++)
for(int r=0;r<=cnt[0];r++)
for(int g=0;g<=cnt[1];g++)
dp[f][r][g]=tmp[f][r][g];
}
int ans=inf;
for(int f=0;f<3;f++) ckmn(ans,dp[f][0][0]);
printf("%i\n",ans==inf?-1:ans);*/
return 0;
}
Compilation message
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:66:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&n);
~~~~~^~~~~~~~~
joi2019_ho_t3.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s",s+1);
~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
508 KB |
Output is correct |
5 |
Correct |
2 ms |
632 KB |
Output is correct |
6 |
Correct |
2 ms |
760 KB |
Output is correct |
7 |
Correct |
2 ms |
760 KB |
Output is correct |
8 |
Correct |
2 ms |
760 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
508 KB |
Output is correct |
11 |
Correct |
2 ms |
760 KB |
Output is correct |
12 |
Correct |
2 ms |
760 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
764 KB |
Output is correct |
15 |
Correct |
2 ms |
632 KB |
Output is correct |
16 |
Correct |
2 ms |
632 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
508 KB |
Output is correct |
5 |
Correct |
2 ms |
632 KB |
Output is correct |
6 |
Correct |
2 ms |
760 KB |
Output is correct |
7 |
Correct |
2 ms |
760 KB |
Output is correct |
8 |
Correct |
2 ms |
760 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
508 KB |
Output is correct |
11 |
Correct |
2 ms |
760 KB |
Output is correct |
12 |
Correct |
2 ms |
760 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
764 KB |
Output is correct |
15 |
Correct |
2 ms |
632 KB |
Output is correct |
16 |
Correct |
2 ms |
632 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
3704 KB |
Output is correct |
19 |
Correct |
5 ms |
3064 KB |
Output is correct |
20 |
Correct |
5 ms |
3576 KB |
Output is correct |
21 |
Correct |
6 ms |
3836 KB |
Output is correct |
22 |
Correct |
4 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
3320 KB |
Output is correct |
24 |
Correct |
4 ms |
2424 KB |
Output is correct |
25 |
Correct |
3 ms |
1656 KB |
Output is correct |
26 |
Correct |
3 ms |
1912 KB |
Output is correct |
27 |
Correct |
5 ms |
3856 KB |
Output is correct |
28 |
Correct |
5 ms |
3704 KB |
Output is correct |
29 |
Correct |
5 ms |
3576 KB |
Output is correct |
30 |
Correct |
5 ms |
3448 KB |
Output is correct |
31 |
Correct |
5 ms |
3192 KB |
Output is correct |
32 |
Correct |
5 ms |
3576 KB |
Output is correct |
33 |
Correct |
3 ms |
2040 KB |
Output is correct |
34 |
Correct |
4 ms |
2296 KB |
Output is correct |
35 |
Correct |
5 ms |
3704 KB |
Output is correct |
36 |
Correct |
5 ms |
3092 KB |
Output is correct |
37 |
Correct |
4 ms |
2680 KB |
Output is correct |
38 |
Correct |
5 ms |
3064 KB |
Output is correct |
39 |
Correct |
5 ms |
3704 KB |
Output is correct |
40 |
Correct |
2 ms |
632 KB |
Output is correct |
41 |
Correct |
3 ms |
1400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
8 ms |
6264 KB |
Output is correct |
3 |
Correct |
7 ms |
6264 KB |
Output is correct |
4 |
Correct |
7 ms |
6292 KB |
Output is correct |
5 |
Correct |
7 ms |
6264 KB |
Output is correct |
6 |
Correct |
7 ms |
6264 KB |
Output is correct |
7 |
Correct |
7 ms |
6264 KB |
Output is correct |
8 |
Correct |
7 ms |
6264 KB |
Output is correct |
9 |
Correct |
7 ms |
6264 KB |
Output is correct |
10 |
Correct |
9 ms |
6264 KB |
Output is correct |
11 |
Correct |
8 ms |
6264 KB |
Output is correct |
12 |
Correct |
5 ms |
3448 KB |
Output is correct |
13 |
Correct |
5 ms |
4344 KB |
Output is correct |
14 |
Correct |
6 ms |
5240 KB |
Output is correct |
15 |
Correct |
7 ms |
6264 KB |
Output is correct |
16 |
Correct |
7 ms |
6264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
508 KB |
Output is correct |
5 |
Correct |
2 ms |
632 KB |
Output is correct |
6 |
Correct |
2 ms |
760 KB |
Output is correct |
7 |
Correct |
2 ms |
760 KB |
Output is correct |
8 |
Correct |
2 ms |
760 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
508 KB |
Output is correct |
11 |
Correct |
2 ms |
760 KB |
Output is correct |
12 |
Correct |
2 ms |
760 KB |
Output is correct |
13 |
Correct |
2 ms |
632 KB |
Output is correct |
14 |
Correct |
3 ms |
764 KB |
Output is correct |
15 |
Correct |
2 ms |
632 KB |
Output is correct |
16 |
Correct |
2 ms |
632 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
3704 KB |
Output is correct |
19 |
Correct |
5 ms |
3064 KB |
Output is correct |
20 |
Correct |
5 ms |
3576 KB |
Output is correct |
21 |
Correct |
6 ms |
3836 KB |
Output is correct |
22 |
Correct |
4 ms |
2808 KB |
Output is correct |
23 |
Correct |
5 ms |
3320 KB |
Output is correct |
24 |
Correct |
4 ms |
2424 KB |
Output is correct |
25 |
Correct |
3 ms |
1656 KB |
Output is correct |
26 |
Correct |
3 ms |
1912 KB |
Output is correct |
27 |
Correct |
5 ms |
3856 KB |
Output is correct |
28 |
Correct |
5 ms |
3704 KB |
Output is correct |
29 |
Correct |
5 ms |
3576 KB |
Output is correct |
30 |
Correct |
5 ms |
3448 KB |
Output is correct |
31 |
Correct |
5 ms |
3192 KB |
Output is correct |
32 |
Correct |
5 ms |
3576 KB |
Output is correct |
33 |
Correct |
3 ms |
2040 KB |
Output is correct |
34 |
Correct |
4 ms |
2296 KB |
Output is correct |
35 |
Correct |
5 ms |
3704 KB |
Output is correct |
36 |
Correct |
5 ms |
3092 KB |
Output is correct |
37 |
Correct |
4 ms |
2680 KB |
Output is correct |
38 |
Correct |
5 ms |
3064 KB |
Output is correct |
39 |
Correct |
5 ms |
3704 KB |
Output is correct |
40 |
Correct |
2 ms |
632 KB |
Output is correct |
41 |
Correct |
3 ms |
1400 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
8 ms |
6264 KB |
Output is correct |
44 |
Correct |
7 ms |
6264 KB |
Output is correct |
45 |
Correct |
7 ms |
6292 KB |
Output is correct |
46 |
Correct |
7 ms |
6264 KB |
Output is correct |
47 |
Correct |
7 ms |
6264 KB |
Output is correct |
48 |
Correct |
7 ms |
6264 KB |
Output is correct |
49 |
Correct |
7 ms |
6264 KB |
Output is correct |
50 |
Correct |
7 ms |
6264 KB |
Output is correct |
51 |
Correct |
9 ms |
6264 KB |
Output is correct |
52 |
Correct |
8 ms |
6264 KB |
Output is correct |
53 |
Correct |
5 ms |
3448 KB |
Output is correct |
54 |
Correct |
5 ms |
4344 KB |
Output is correct |
55 |
Correct |
6 ms |
5240 KB |
Output is correct |
56 |
Correct |
7 ms |
6264 KB |
Output is correct |
57 |
Correct |
7 ms |
6264 KB |
Output is correct |
58 |
Correct |
244 ms |
98324 KB |
Output is correct |
59 |
Correct |
261 ms |
120860 KB |
Output is correct |
60 |
Correct |
263 ms |
112664 KB |
Output is correct |
61 |
Correct |
257 ms |
109264 KB |
Output is correct |
62 |
Correct |
17 ms |
16376 KB |
Output is correct |
63 |
Correct |
26 ms |
26232 KB |
Output is correct |
64 |
Correct |
88 ms |
74488 KB |
Output is correct |
65 |
Correct |
151 ms |
101532 KB |
Output is correct |
66 |
Correct |
254 ms |
110712 KB |
Output is correct |
67 |
Correct |
303 ms |
105704 KB |
Output is correct |
68 |
Correct |
283 ms |
113912 KB |
Output is correct |
69 |
Correct |
269 ms |
110200 KB |
Output is correct |
70 |
Correct |
285 ms |
117880 KB |
Output is correct |
71 |
Correct |
250 ms |
107640 KB |
Output is correct |
72 |
Correct |
24 ms |
18040 KB |
Output is correct |
73 |
Correct |
6 ms |
4728 KB |
Output is correct |