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 <iostream>
using namespace std;
int n,dp[401][401][401][3],cnt[3][401][3],r,g,y;
string s;
int main(){
ios_base::sync_with_stdio(NULL);cin.tie(nullptr);
cin >> n >> s;
for (int i=0;i<n;i++){
if (s[i]=='R'){
cnt[0][++r][1]=g;
cnt[0][r][2]=y;
}
if (s[i]=='G'){
cnt[1][++g][0]=r;
cnt[1][g][2]=y;
}
if (s[i]=='Y'){
cnt[2][++y][1]=g;
cnt[2][y][0]=r;
}
}
for (int i=0;i<=r;i++)
for (int j=0;j<=g;j++)
for (int k=0;k<=y;k++)
for (int l=0;l<3;l++)
if (i+j+k)
dp[i][j][k][l]=1000000000;
for (int i=0;i<=r;i++)
for (int j=0;j<=g;j++)
for (int k=0;k<=y;k++){
if (i<r)
dp[i+1][j][k][0]=min(dp[i+1][j][k][0],min(dp[i][j][k][1],dp[i][j][k][2])+abs(cnt[0][i+1][1]-j)+abs(cnt[0][i+1][2]-k));
if (j<g)
dp[i][j+1][k][1]=min(dp[i][j+1][k][1],min(dp[i][j][k][0],dp[i][j][k][2])+abs(cnt[1][j+1][0]-i)+abs(cnt[1][j+1][2]-k));
if (k<y)
dp[i][j][k+1][2]=min(dp[i][j][k+1][2],min(dp[i][j][k][1],dp[i][j][k][0])+abs(cnt[2][k+1][1]-j)+abs(cnt[2][k+1][0]-i));
}
int res=min(min(dp[r][g][y][0],dp[r][g][y][1]),dp[r][g][y][2]);
cout << (res==1000000000?-1:res>>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... |