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>
#define ll int
#define str string
#define ins insert
#define ld long double
#define pb push_back
#define pf push_front
#define pof pop_front()
#define pob pop_back()
#define lb lower_bound
#define ub upper_bound
#define endl "\n"
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define sz size()
#define vll vector<ll>
#define bc back()
#define arr array
#define pll vector<pair<ll,ll>>
using namespace std;
template <class _T>
bool chmin(_T &x, const _T &y){
bool f=0;
if (x>y){x=y;f=1;}
return f;
}
template <class _T>
bool chmax(_T &x, const _T &y){
bool f=0;
if (x<y){x=y;f=1;}
return f;
}
//void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);}
void start(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
const ll inf=1e9;
const ll mod=1e9+7;
const ll N=402;
const ld eps=1e-9;
ll dp[N][N][N][3];
void solve(){
ll i,j,q,k;
ll n;
cin>>n;
ll a[n+5];
str s;
cin>>s;
vector<vll> v(3);
for(i=0;i<n;i++){
ll x;
if(s[i]=='R')x=0;
if(s[i]=='G')x=1;
if(s[i]=='Y')x=2;
a[i+1]=x;
v[x].pb(i+1);
}
ll R=v[0].sz,G=v[1].sz,Y=v[2].sz;
for(i=0;i<=R;i++)
for(j=0;j<=G;j++)
for(q=0;q<=Y;q++)
for(k=0;k<3;k++)
dp[i][j][q][k]=inf;
dp[0][0][0][0]=0;
dp[0][0][0][1]=0;
dp[0][0][0][2]=0;
ll pref[n+5][3];
memset(pref,0,sizeof(pref));
for(i=1;i<=n;i++){
for(j=0;j<3;j++){
pref[i][j]=pref[i-1][j];
if(a[i]==j)pref[i][j]++;
}
}
for(i=0;i<=R;i++){
for(j=0;j<=G;j++){
for(q=0;q<=Y;q++){
for(k=0;k<3;k++){
if(i<R && k!=0){
chmin(dp[i+1][j][q][0],dp[i][j][q][k]+max(0,pref[v[0][i]][1]-j)+max(0,pref[v[0][i]][2]-q));
}
if(j<G && k!=1){
chmin(dp[i][j+1][q][1],dp[i][j][q][k]+max(0,pref[v[1][j]][0]-i)+max(0,pref[v[1][j]][2]-q));
}
if(q<Y && k!=2){
chmin(dp[i][j][q+1][2],dp[i][j][q][k]+max(0,pref[v[2][q]][0]-i)+max(0,pref[v[2][q]][1]-j));
}
}
}
}
}
ll ans=inf;
for(i=0;i<3;i++){
chmin(ans,dp[R][G][Y][i]);
}
if(ans==inf)cout<<-1<<endl;
else cout<<ans<<endl;
}
signed main(){
//start();
ll t=1;
//cin>>t;
while(t--) solve();
return 0;
}
/*
*/
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'void solve()':
joi2019_ho_t3.cpp:60:12: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
60 | v[x].pb(i+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... |