#include <iostream>
#include <fstream>
using namespace std;
const int nmax=405;
int dp[2][nmax][nmax][3];
int ap[3],nr[3];
int p[3][nmax][3],a[3][nmax];
int n,i,j,k,use,oth,ch,nxt,ans;
string s;
int norm(char c)
{
if(c=='R') return 0;
if(c=='G') return 1;
return 2;
}
int actual_pos(int wh)
{
int ret=a[wh][ap[wh]];
for(int oth=0;oth<3;oth++)
if(oth!=wh)
ret+=max(0,ap[oth]-p[wh][ap[wh]][oth]);
return ret;
}
void prp(int &a,int b)
{
a=min(a,b);
}
int main()
{
//freopen("data.in","r",stdin);
ios_base::sync_with_stdio(false);
cin>>n;
cin>>s;s=" "+s;
for(i=1;i<=n;i++)
a[norm(s[i])][++nr[norm(s[i])]]=i;
for(i=0;i<3;i++)
for(j=1;j<=nr[i];j++)
for(oth=0;oth<3;oth++)
{
p[i][j][oth]=p[i][j-1][oth];
while(p[i][j][oth]<=nr[oth]&&a[oth][p[i][j][oth]]<a[i][j])
p[i][j][oth]++;
p[i][j][oth]--;
}
for(i=0;i<2;i++)
for(j=0;j<=n;j++)
for(k=0;k<=n;k++)
for(ch=0;ch<3;ch++)
dp[i][j][k][ch]=(1<<30);
for(i=0;i<3;i++)
dp[0][0][0][i]=0;
for(i=1;i<=n;i++)
{
use=1-use;
for(j=0;j<=i-1;j++)
for(k=0;k<=i-1-j;k++)
for(ch=0;ch<3;ch++)
if(dp[1-use][j][k][ch]<(1<<30))
{
ap[0]=j;ap[1]=k;ap[2]=i-1-j-k;
for(nxt=0;nxt<3;nxt++)
if(nxt!=ch&&ap[nxt]<nr[nxt])
{
ap[nxt]++;
prp(dp[use][ap[0]][ap[1]][nxt],dp[1-use][j][k][ch]+actual_pos(nxt)-i);
ap[nxt]--;
}
}
for(j=0;j<=i-1;j++)
for(k=0;k<=i-j-1;k++)
for(ch=0;ch<3;ch++)
dp[1-use][j][k][ch]=(1<<30);
}
ans=(1<<30);
for(i=0;i<3;i++)
ans=min(ans,dp[use][nr[0]][nr[1]][i]);
if(ans==(1<<30)) ans=-1;
cout<<ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
3 ms |
896 KB |
Output is correct |
19 |
Correct |
3 ms |
896 KB |
Output is correct |
20 |
Correct |
4 ms |
896 KB |
Output is correct |
21 |
Correct |
4 ms |
896 KB |
Output is correct |
22 |
Correct |
3 ms |
896 KB |
Output is correct |
23 |
Correct |
4 ms |
896 KB |
Output is correct |
24 |
Correct |
3 ms |
896 KB |
Output is correct |
25 |
Correct |
3 ms |
896 KB |
Output is correct |
26 |
Correct |
3 ms |
896 KB |
Output is correct |
27 |
Correct |
3 ms |
896 KB |
Output is correct |
28 |
Correct |
3 ms |
896 KB |
Output is correct |
29 |
Correct |
3 ms |
896 KB |
Output is correct |
30 |
Correct |
3 ms |
896 KB |
Output is correct |
31 |
Correct |
3 ms |
896 KB |
Output is correct |
32 |
Correct |
3 ms |
896 KB |
Output is correct |
33 |
Correct |
3 ms |
896 KB |
Output is correct |
34 |
Correct |
4 ms |
896 KB |
Output is correct |
35 |
Correct |
4 ms |
896 KB |
Output is correct |
36 |
Correct |
3 ms |
896 KB |
Output is correct |
37 |
Correct |
3 ms |
896 KB |
Output is correct |
38 |
Correct |
3 ms |
896 KB |
Output is correct |
39 |
Correct |
4 ms |
896 KB |
Output is correct |
40 |
Correct |
2 ms |
896 KB |
Output is correct |
41 |
Correct |
4 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
58 ms |
4096 KB |
Output is correct |
3 |
Correct |
60 ms |
4236 KB |
Output is correct |
4 |
Correct |
50 ms |
4096 KB |
Output is correct |
5 |
Correct |
61 ms |
4208 KB |
Output is correct |
6 |
Correct |
57 ms |
4096 KB |
Output is correct |
7 |
Correct |
59 ms |
4224 KB |
Output is correct |
8 |
Correct |
59 ms |
4096 KB |
Output is correct |
9 |
Correct |
59 ms |
4224 KB |
Output is correct |
10 |
Correct |
59 ms |
4096 KB |
Output is correct |
11 |
Correct |
55 ms |
4224 KB |
Output is correct |
12 |
Correct |
11 ms |
2304 KB |
Output is correct |
13 |
Correct |
21 ms |
2944 KB |
Output is correct |
14 |
Correct |
33 ms |
3452 KB |
Output is correct |
15 |
Correct |
56 ms |
4096 KB |
Output is correct |
16 |
Correct |
93 ms |
4216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
2 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
3 ms |
896 KB |
Output is correct |
19 |
Correct |
3 ms |
896 KB |
Output is correct |
20 |
Correct |
4 ms |
896 KB |
Output is correct |
21 |
Correct |
4 ms |
896 KB |
Output is correct |
22 |
Correct |
3 ms |
896 KB |
Output is correct |
23 |
Correct |
4 ms |
896 KB |
Output is correct |
24 |
Correct |
3 ms |
896 KB |
Output is correct |
25 |
Correct |
3 ms |
896 KB |
Output is correct |
26 |
Correct |
3 ms |
896 KB |
Output is correct |
27 |
Correct |
3 ms |
896 KB |
Output is correct |
28 |
Correct |
3 ms |
896 KB |
Output is correct |
29 |
Correct |
3 ms |
896 KB |
Output is correct |
30 |
Correct |
3 ms |
896 KB |
Output is correct |
31 |
Correct |
3 ms |
896 KB |
Output is correct |
32 |
Correct |
3 ms |
896 KB |
Output is correct |
33 |
Correct |
3 ms |
896 KB |
Output is correct |
34 |
Correct |
4 ms |
896 KB |
Output is correct |
35 |
Correct |
4 ms |
896 KB |
Output is correct |
36 |
Correct |
3 ms |
896 KB |
Output is correct |
37 |
Correct |
3 ms |
896 KB |
Output is correct |
38 |
Correct |
3 ms |
896 KB |
Output is correct |
39 |
Correct |
4 ms |
896 KB |
Output is correct |
40 |
Correct |
2 ms |
896 KB |
Output is correct |
41 |
Correct |
4 ms |
896 KB |
Output is correct |
42 |
Correct |
2 ms |
384 KB |
Output is correct |
43 |
Correct |
58 ms |
4096 KB |
Output is correct |
44 |
Correct |
60 ms |
4236 KB |
Output is correct |
45 |
Correct |
50 ms |
4096 KB |
Output is correct |
46 |
Correct |
61 ms |
4208 KB |
Output is correct |
47 |
Correct |
57 ms |
4096 KB |
Output is correct |
48 |
Correct |
59 ms |
4224 KB |
Output is correct |
49 |
Correct |
59 ms |
4096 KB |
Output is correct |
50 |
Correct |
59 ms |
4224 KB |
Output is correct |
51 |
Correct |
59 ms |
4096 KB |
Output is correct |
52 |
Correct |
55 ms |
4224 KB |
Output is correct |
53 |
Correct |
11 ms |
2304 KB |
Output is correct |
54 |
Correct |
21 ms |
2944 KB |
Output is correct |
55 |
Correct |
33 ms |
3452 KB |
Output is correct |
56 |
Correct |
56 ms |
4096 KB |
Output is correct |
57 |
Correct |
93 ms |
4216 KB |
Output is correct |
58 |
Correct |
124 ms |
4224 KB |
Output is correct |
59 |
Correct |
118 ms |
4224 KB |
Output is correct |
60 |
Correct |
118 ms |
4096 KB |
Output is correct |
61 |
Correct |
147 ms |
4224 KB |
Output is correct |
62 |
Correct |
56 ms |
4224 KB |
Output is correct |
63 |
Correct |
65 ms |
4348 KB |
Output is correct |
64 |
Correct |
64 ms |
4224 KB |
Output is correct |
65 |
Correct |
75 ms |
4216 KB |
Output is correct |
66 |
Correct |
131 ms |
4216 KB |
Output is correct |
67 |
Correct |
107 ms |
4224 KB |
Output is correct |
68 |
Correct |
134 ms |
4196 KB |
Output is correct |
69 |
Correct |
155 ms |
4224 KB |
Output is correct |
70 |
Correct |
118 ms |
4196 KB |
Output is correct |
71 |
Correct |
158 ms |
4216 KB |
Output is correct |
72 |
Correct |
57 ms |
4224 KB |
Output is correct |
73 |
Correct |
64 ms |
4224 KB |
Output is correct |