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 nl '\n'
inline void solve(){
int n;
cin>>n;
string s;
cin>>s;
vector<int> a(n);
for(int i=0; i<n; i++){
if(s[i] == 'R') a[i] = 0;
else if(s[i] == 'G') a[i] = 1;
else a[i] = 2;
}
auto b = a;
sort(b.begin(), b.end());
int ans = 1e18;
do{
auto c = b;
bool tf = true;
for(int i=0; i<n-1; i++){
if(c[i] == c[i+1]){
tf = false;
break;
}
}
if(!tf) continue;
int subans = 0;
for(int i=0; i<n; i++){
if(c[i] == a[i]) continue;
tf = false;
for(int j=i+1; j<n; j++){
if(c[j] != a[i]) continue;
for(int k=j; k>i; k--){
swap(c[k], c[k-1]);
subans++;
}
tf = true;
break;
}
if(!tf){
subans = 1e18;
break;
}
}
ans = min(ans, subans);
}while(next_permutation(b.begin(), b.end()));
cout<<(ans == 1e18 ? -1 : ans);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(NULL);cout.tie(NULL);
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... |