#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0); cin.tie(0)
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("popcnt")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll MAX=2e5+10,P=1e9+7;
const ll INF=0x3f3f3f3f,oo=0x3f3f3f3f3f3f3f3f;
const ldb eps=1e-6;
const ldb PI=acos(-1.0);
const int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
template<typename T>
using vvector = vector<vector<T>>;
int dp[210][210][210][3];
int p[3][410];
int main() {
speed;
int n;
cin>>n;
string s;
cin>>s;
s='-'+s;
int cnt[3]={0,0,0};
for (int i=1;i<=n;i++) {
if (s[i]=='R') {
cnt[0]++;
p[0][cnt[0]]=i;
}
else if (s[i]=='G') {
cnt[1]++;
p[1][cnt[1]]=i;
}
else if (s[i]=='Y') {
cnt[2]++;
p[2][cnt[2]]=i;
}
}
if (cnt[0]>(n+1)/2 or cnt[1]>(n+1)/2 or cnt[2]>(n+1)/2) {
cout<<"-1\n";
return 0;
}
memset(dp,0x3f,sizeof(dp));
dp[0][0][0][0]=0;
dp[0][0][0][1]=0;
dp[0][0][0][2]=0;
for (int i=0;i<=cnt[0];i++) {
for (int j=0;j<=cnt[1];j++) {
for (int k=0;k<=cnt[2];k++) {
if (i!=0) dp[i][j][k][0]=min(dp[i-1][j][k][1],dp[i-1][j][k][2])+max(i+j+k-p[0][i],0);
if (j!=0) dp[i][j][k][1]=min(dp[i][j-1][k][0],dp[i][j-1][k][2])+max(i+j+k-p[1][j],0);
if (k!=0) dp[i][j][k][2]=min(dp[i][j][k-1][1],dp[i][j][k-1][0])+max(i+j+k-p[2][k],0);
// cout<<i<<" "<<j<<" "<<k<<" ijk\n";
// cout<<p[0][i]<<" "<<p[1][j]<<" "<<p[2][k]<<"\n";
// cout<<dp[i][j][k][0]<<"\n"<<dp[i][j][k][1]<<"\n"<<dp[i][j][k][2]<<"\n";
}
}
}
cout<<min({dp[cnt[0]][cnt[1]][cnt[2]][0],dp[cnt[0]][cnt[1]][cnt[2]][1],dp[cnt[0]][cnt[1]][cnt[2]][2]})<<"\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... |