#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define sz size()
#define all(v) v.begin(),v.end()
#define fi first
#define se second
const int N = 1e5;
const int mod = 1e9+7;
const int INF = 1e9;
const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};
int n,cnt1,cnt2,cnt3;
string S;
int ans=INF;
vector<char> v={'R','G','Y'};
void calc(string t){
if(t.sz==n){
t="!"+t;
string s=S;
int res=0;
for(int i = 1; i <= n; i++){
if(t[i]!=s[i]){
int pos=-1;
for(int j = i+1; j <= n; j++){
if(t[i]==s[j]){
pos=j;
break;
}
}
for(int j = pos-1; j >= i; j--){
swap(s[j],s[j+1]);
res++;
}
}
}
// cout<<t<<" "<<res<<"\n";
for(int i = 1; i <= n; i++){
if(t[i]!=s[i]){
return;
}
}
ans=min(ans,res);
return;
}
for(int i = 0; i < 3; i++){
if(t.back()!=v[i]){
calc(t+v[i]);
}
}
}
void solve() {
cin>>n>>S;
S="!"+S;
for(int i = 1; i <= n; i++){
if(S[i]=='R') cnt1++;
if(S[i]=='G') cnt2++;
if(S[i]=='Y') cnt3++;
}
calc("");
if(ans==INF)
ans=-1;
cout<<ans<<"\n";
}
int main() {
//freopen("cowrun.in","r",stdin);
//freopen("cowrun.out","w",stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
//cin>>t;
while(t--) {
solve();
}
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... |