///10:32
///10:55
///-10min
///-10min
#include<bits/stdc++.h>
#define INF 100000000
#define maxn 405
using namespace std;
map<char,int>br;
int n;
string s;
int dp[maxn][maxn][maxn][3];
int col[maxn][3];
int tot[3];
int num[maxn][3];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
br['R']=0;
br['G']=1;
br['Y']=2;
cin>>n;
cin>>s;
tot[0]=tot[1]=tot[2]=0;
for(int i=0;i<n;i++)
{
int boja=br[s[i]];
//cout<<boja<<" ";
tot[boja]++;
col[tot[boja]][boja]=i;
if(i==0)
{
for(int j=0;j<=2;j++)
{
num[i][j]=0;
}
}
else
{
for(int j=0;j<=2;j++)
{
num[i][j]=num[i-1][j];
}
}
num[i][boja]++;
}
/*cout<<endl;
for(int i=0;i<=2;i++)
{
cout<<tot[i]<<" ";
for(int j=1;j<=tot[i];j++)
{
cout<<col[j][i]<<" ";
}
cout<<endl;
}*/
int red[3];
for(red[0]=0;red[0]<=tot[0];red[0]++)
{
for(red[1]=0;red[1]<=tot[1];red[1]++)
{
for(red[2]=0;red[2]<=tot[2];red[2]++)
{
for(int i=0;i<=2;i++)
{
dp[ red[0] ][ red[1] ][ red[2] ][i]=INF;
if(red[i]==0)continue;
int tre[3];
for(int j=0;j<=2;j++)
{
tre[j]=red[j];
}
tre[i]--;
int pre_poz=col[red[i]][i];
int left_ok=0;
for(int j=0;j<=2;j++)
{
int broj=0;
if(pre_poz!=0)
{
broj=num[pre_poz-1][j];
}
left_ok+=min(tre[j],broj);
}
int poz=red[0]+red[1]+red[2]-1;
int dod= (pre_poz+poz) - 2*left_ok;
int ost1=(i+1)%3;
int ost2=(i+2)%3;
if(poz==0)
{
dp[ red[0] ][ red[1] ][ red[2] ][i]=dod;
continue;
}
dp[ red[0] ][ red[1] ][ red[2] ][i]=min(dp[ red[0] ][ red[1] ][ red[2] ][i], dp[ tre[0] ][ tre[1] ][ tre[2] ][ost1] + dod);
dp[ red[0] ][ red[1] ][ red[2] ][i]=min(dp[ red[0] ][ red[1] ][ red[2] ][i], dp[ tre[0] ][ tre[1] ][ tre[2] ][ost2] + dod);
}
}
}
}
int sol=INF;
for(int i=0;i<=2;i++)
{
sol=min(sol,dp[ tot[0] ][ tot[1] ][ tot[2] ][i]);
}
if(sol>=INF)cout<<"-1"<<endl;
else
{
sol/=2;
cout<<sol<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 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 |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
2424 KB |
Output is correct |
19 |
Correct |
3 ms |
1912 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
4 ms |
2400 KB |
Output is correct |
22 |
Correct |
3 ms |
1784 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
3 ms |
1656 KB |
Output is correct |
25 |
Correct |
5 ms |
3960 KB |
Output is correct |
26 |
Correct |
5 ms |
4188 KB |
Output is correct |
27 |
Correct |
4 ms |
3196 KB |
Output is correct |
28 |
Correct |
4 ms |
2424 KB |
Output is correct |
29 |
Correct |
4 ms |
2296 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
4 ms |
2040 KB |
Output is correct |
32 |
Correct |
4 ms |
2424 KB |
Output is correct |
33 |
Correct |
5 ms |
3960 KB |
Output is correct |
34 |
Correct |
5 ms |
3576 KB |
Output is correct |
35 |
Correct |
4 ms |
2680 KB |
Output is correct |
36 |
Correct |
3 ms |
1912 KB |
Output is correct |
37 |
Correct |
3 ms |
1784 KB |
Output is correct |
38 |
Correct |
4 ms |
2168 KB |
Output is correct |
39 |
Correct |
4 ms |
2396 KB |
Output is correct |
40 |
Correct |
2 ms |
504 KB |
Output is correct |
41 |
Correct |
4 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
121 ms |
163044 KB |
Output is correct |
3 |
Correct |
120 ms |
162296 KB |
Output is correct |
4 |
Correct |
143 ms |
162936 KB |
Output is correct |
5 |
Correct |
121 ms |
163064 KB |
Output is correct |
6 |
Correct |
120 ms |
163036 KB |
Output is correct |
7 |
Correct |
120 ms |
162232 KB |
Output is correct |
8 |
Correct |
136 ms |
162260 KB |
Output is correct |
9 |
Correct |
119 ms |
161400 KB |
Output is correct |
10 |
Correct |
120 ms |
162936 KB |
Output is correct |
11 |
Correct |
121 ms |
162996 KB |
Output is correct |
12 |
Correct |
34 ms |
44152 KB |
Output is correct |
13 |
Correct |
59 ms |
77176 KB |
Output is correct |
14 |
Correct |
84 ms |
111480 KB |
Output is correct |
15 |
Correct |
121 ms |
163172 KB |
Output is correct |
16 |
Correct |
122 ms |
163036 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 |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Correct |
2 ms |
632 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
2 ms |
504 KB |
Output is correct |
16 |
Correct |
2 ms |
504 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
4 ms |
2424 KB |
Output is correct |
19 |
Correct |
3 ms |
1912 KB |
Output is correct |
20 |
Correct |
4 ms |
2296 KB |
Output is correct |
21 |
Correct |
4 ms |
2400 KB |
Output is correct |
22 |
Correct |
3 ms |
1784 KB |
Output is correct |
23 |
Correct |
4 ms |
2168 KB |
Output is correct |
24 |
Correct |
3 ms |
1656 KB |
Output is correct |
25 |
Correct |
5 ms |
3960 KB |
Output is correct |
26 |
Correct |
5 ms |
4188 KB |
Output is correct |
27 |
Correct |
4 ms |
3196 KB |
Output is correct |
28 |
Correct |
4 ms |
2424 KB |
Output is correct |
29 |
Correct |
4 ms |
2296 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
4 ms |
2040 KB |
Output is correct |
32 |
Correct |
4 ms |
2424 KB |
Output is correct |
33 |
Correct |
5 ms |
3960 KB |
Output is correct |
34 |
Correct |
5 ms |
3576 KB |
Output is correct |
35 |
Correct |
4 ms |
2680 KB |
Output is correct |
36 |
Correct |
3 ms |
1912 KB |
Output is correct |
37 |
Correct |
3 ms |
1784 KB |
Output is correct |
38 |
Correct |
4 ms |
2168 KB |
Output is correct |
39 |
Correct |
4 ms |
2396 KB |
Output is correct |
40 |
Correct |
2 ms |
504 KB |
Output is correct |
41 |
Correct |
4 ms |
2296 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Correct |
121 ms |
163044 KB |
Output is correct |
44 |
Correct |
120 ms |
162296 KB |
Output is correct |
45 |
Correct |
143 ms |
162936 KB |
Output is correct |
46 |
Correct |
121 ms |
163064 KB |
Output is correct |
47 |
Correct |
120 ms |
163036 KB |
Output is correct |
48 |
Correct |
120 ms |
162232 KB |
Output is correct |
49 |
Correct |
136 ms |
162260 KB |
Output is correct |
50 |
Correct |
119 ms |
161400 KB |
Output is correct |
51 |
Correct |
120 ms |
162936 KB |
Output is correct |
52 |
Correct |
121 ms |
162996 KB |
Output is correct |
53 |
Correct |
34 ms |
44152 KB |
Output is correct |
54 |
Correct |
59 ms |
77176 KB |
Output is correct |
55 |
Correct |
84 ms |
111480 KB |
Output is correct |
56 |
Correct |
121 ms |
163172 KB |
Output is correct |
57 |
Correct |
122 ms |
163036 KB |
Output is correct |
58 |
Correct |
174 ms |
76280 KB |
Output is correct |
59 |
Correct |
192 ms |
96632 KB |
Output is correct |
60 |
Correct |
185 ms |
88000 KB |
Output is correct |
61 |
Correct |
181 ms |
85080 KB |
Output is correct |
62 |
Correct |
128 ms |
160632 KB |
Output is correct |
63 |
Correct |
136 ms |
160248 KB |
Output is correct |
64 |
Correct |
181 ms |
150172 KB |
Output is correct |
65 |
Correct |
189 ms |
138568 KB |
Output is correct |
66 |
Correct |
181 ms |
86212 KB |
Output is correct |
67 |
Correct |
179 ms |
82896 KB |
Output is correct |
68 |
Correct |
184 ms |
88720 KB |
Output is correct |
69 |
Correct |
184 ms |
85472 KB |
Output is correct |
70 |
Correct |
188 ms |
92656 KB |
Output is correct |
71 |
Correct |
179 ms |
84020 KB |
Output is correct |
72 |
Correct |
63 ms |
41740 KB |
Output is correct |
73 |
Correct |
11 ms |
2936 KB |
Output is correct |