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;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef long double ld;
const ll MOD=1e9+7;
const ll N=4e2+5;
const ld pi=3.14159265359;
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(a) (ll)a.size()
ll R,G,Y;
ll n,dp[N][N][N][3],p[N][3];
string s;
vector<ll> v[3];
ll f(ll id,ll ty,ll ha){
return max(0LL,ha-p[id][ty]);
}
int main(){
//ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>s;
REP(i,SZ(s))(s[i]=='R'?v[0]:(s[i]=='G'?v[1]:v[2])).pb(i),(s[i]=='R'?R:(s[i]=='G'?G:Y))++;
REP(i,n){
REP(ty,3)p[i][ty]=(i?p[i-1][ty]:0LL);
(s[i]=='R'?p[i][0]:(s[i]=='G'?p[i][1]:p[i][2]))++;
}
REP(i,2)REP(j,N)REP(k,N)REP(ty,3)dp[i][j][k][ty]=(1LL<<60);
dp[0][0][0][0]=dp[0][0][0][1]=dp[0][0][0][2]=0;
REP(i,n){
REP(j,G+1){
REP(k,Y+1){
if(k+j>i||i-j-k>R)continue;
REP(ty,3){
//cout<<i<<" "<<j<<" "<<k<<" "<<ty<<"::"<<dp[i][j][k][ty]<<"\n";
ll x=i-j-k;
if(ty!=0&&x<R)dp[((i+1)&1)][j][k][0]=min(dp[((i+1)&1)][j][k][0],dp[(i&1)][j][k][ty]+(v[0][x]+f(v[0][x],1,j)+f(v[0][x],2,k)-i));
if(ty!=1&&j<G)dp[((i+1)&1)][j+1][k][1]=min(dp[((i+1)&1)][j+1][k][1],dp[(i&1)][j][k][ty]+(v[1][j]+f(v[1][j],0,x)+f(v[1][j],2,k)-i));
if(ty!=2&&k<Y)dp[((i+1)&1)][j][k+1][2]=min(dp[((i+1)&1)][j][k+1][2],dp[(i&1)][j][k][ty]+(v[2][k]+f(v[2][k],0,x)+f(v[2][k],1,j)-i));
}
}
}
REP(j,G+1)REP(k,Y+1)REP(ty,3)dp[(i&1)][j][k][ty]=(1LL<<60);
}
ll ans=min(dp[(n&1)][G][Y][0],min(dp[(n&1)][G][Y][1],dp[(n&1)][G][Y][2]));
cout<<(ans>=(1LL<<60)?-1:ans)<<"\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... |