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;
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 (stderr)
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);
~~~~~^~~~~~~~~~
# | 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... |