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 int long long
#define OYY 1000000005
#define mod 998244353
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mid (start+end)/2
#define lim 405
#define fi first
#define se second
int n;// 1 2 3
vector<int> v[4];
int dp[lim][lim][lim][4];
int gec[4][lim][4][lim];// renk index diğer index
inline int dpp(int red,int gre,int yel,int sira,int renk){
if(sira>=n){
return 0;
}
if(~dp[red][gre][yel][renk])return dp[red][gre][yel][renk];
int yum=OYY;
if(renk!=1 && red<(int)v[1].size()){
int deg=0,deg2=0;
if(gre>0)deg=gec[1][red][2][gre-1];
if(yel>0)deg2=gec[1][red][3][yel-1];
yum=min(yum,dpp(red+1,gre,yel,sira+1,1)+v[1][red]+deg+deg2-sira);
}
if(renk!=2 && gre<(int)v[2].size()){
int deg=0,deg2=0;
if(red>0)deg=gec[2][gre][1][red-1];
if(yel>0)deg2=gec[2][gre][3][yel-1];
yum=min(yum,dpp(red,gre+1,yel,sira+1,2)+v[2][gre]+deg+deg2-sira);
}
if(renk!=3 && yel<(int)v[3].size()){
int deg=0,deg2=0;
if(red>0)deg=gec[3][yel][1][red-1];
if(gre>0)deg2=gec[3][yel][2][gre-1];
yum=min(yum,dpp(red,gre,yel+1,sira+1,3)+v[3][yel]+deg+deg2-sira);
}
return dp[red][gre][yel][renk]=yum;
}
int main(){
faster
memset(dp,-1,sizeof(dp));
cin>>n;
string s;cin>>s;
for(int i=0;i<n;i++){
if(s[i]=='R')v[1].push_back(i);
else if(s[i]=='G')v[2].push_back(i);
else v[3].push_back(i);
}
for(int i=0;i<(int)v[1].size();i++){
int tut=0;
for(int j=0;j<(int)v[2].size();j++){
if(v[1][i]<v[2][j])tut++;
gec[1][i][2][j]=tut;
}
}
for(int i=0;i<(int)v[1].size();i++){
int tut=0;
for(int j=0;j<(int)v[3].size();j++){
if(v[1][i]<v[3][j])tut++;
gec[1][i][3][j]=tut;
}
}
for(int i=0;i<(int)v[2].size();i++){
int tut=0;
for(int j=0;j<(int)v[1].size();j++){
if(v[2][i]<v[1][j])tut++;
gec[2][i][1][j]=tut;
}
}
for(int i=0;i<(int)v[2].size();i++){
int tut=0;
for(int j=0;j<(int)v[3].size();j++){
if(v[2][i]<v[3][j])tut++;
gec[2][i][3][j]=tut;
}
}
for(int i=0;i<(int)v[3].size();i++){
int tut=0;
for(int j=0;j<(int)v[1].size();j++){
if(v[3][i]<v[1][j])tut++;
gec[3][i][1][j]=tut;
}
}
for(int i=0;i<(int)v[3].size();i++){
int tut=0;
for(int j=0;j<(int)v[2].size();j++){
if(v[3][i]<v[2][j])tut++;
gec[3][i][2][j]=tut;
}
}
string h="";
int deg=dpp(0,0,0,0,0);
if(deg==OYY)deg=-1;
cout<<deg<<'\n';
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... |