///10:32
///10:55
///
#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 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;
}
/*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 poz=red[0]+red[1]+red[2]-1;
int dod=abs(col[red[i]][i]-poz);
int ost1=(i+1)%3;
int ost2=(i+2)%3;
if(poz==0)
{
dp[ red[0] ][ red[1] ][ red[2] ][i]=dod;
continue;
}
int tre[3];
for(int j=0;j<=2;j++)
{
tre[j]=red[j];
}
tre[i]--;
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 |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
380 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 |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
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 |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
380 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 |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
348 KB |
Output is correct |
2 |
Correct |
128 ms |
162940 KB |
Output is correct |
3 |
Correct |
123 ms |
162240 KB |
Output is correct |
4 |
Correct |
133 ms |
163064 KB |
Output is correct |
5 |
Correct |
145 ms |
163024 KB |
Output is correct |
6 |
Correct |
146 ms |
163192 KB |
Output is correct |
7 |
Correct |
145 ms |
162140 KB |
Output is correct |
8 |
Correct |
144 ms |
162296 KB |
Output is correct |
9 |
Correct |
140 ms |
161344 KB |
Output is correct |
10 |
Correct |
146 ms |
162976 KB |
Output is correct |
11 |
Correct |
148 ms |
163036 KB |
Output is correct |
12 |
Correct |
44 ms |
44152 KB |
Output is correct |
13 |
Correct |
60 ms |
77176 KB |
Output is correct |
14 |
Correct |
86 ms |
111352 KB |
Output is correct |
15 |
Correct |
144 ms |
163068 KB |
Output is correct |
16 |
Correct |
122 ms |
163064 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 |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
380 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 |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
632 KB |
Output is correct |
10 |
Correct |
2 ms |
632 KB |
Output is correct |
11 |
Incorrect |
2 ms |
504 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |