This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define ll long long
ll mod=1000000007;
int inf=1000000007;
ll infl=1000000000000000007;
const int N=407;
int a[N];
int DP[N][N][N][3];
vector<int>V[3];
int P[N][3];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
char x;
cin>>x;
if(x=='R') a[i]=0;
if(x=='G') a[i]=1;
if(x=='Y') a[i]=2;
for(int k=0;k<3;k++) P[i][k]=P[i-1][k];
V[a[i]].pb(i);
P[i][a[i]]++;
}
int x=sz(V[0]),y=sz(V[1]),z=sz(V[2]);
for(int i=0;i<=x;i++)
{
for(int j=0;j<=y;j++)
{
for(int l=0;l<=z;l++)
{
for(int k=0;k<3;k++)
{
DP[i][j][l][k]=inf;
if(i+j+l==0) DP[i][j][l][k]=0;
}
if(i>0)
{
int p=V[0][i-1];
int c=max(0,P[p][1]-j)+max(0,P[p][2]-l);
DP[i][j][l][0]=min(DP[i-1][j][l][1],DP[i-1][j][l][2])+c;
}
if(j>0)
{
int p=V[1][j-1];
int c=max(0,P[p][0]-i)+max(0,P[p][2]-l);
DP[i][j][l][1]=min(DP[i][j-1][l][0],DP[i][j-1][l][2])+c;
}
if(l>0)
{
int p=V[2][l-1];
int c=max(0,P[p][0]-i)+max(0,P[p][1]-j);
DP[i][j][l][2]=min(DP[i][j][l-1][0],DP[i][j][l-1][1])+c;
}
}
}
}
int ans=min({DP[x][y][z][0],DP[x][y][z][1],DP[x][y][z][2]});
if(ans>=inf) cout<<-1;
else cout<<ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |