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;
int ans = 1e9;
int n;
string b;
void brute(int i , int x , int y , int z , string a){
if (i == n) {
int ops = 0;
for (int i = 0 ; i < n ; i++){
for (int j = i ; j < n ; j++){
if (a[j] == b[i]) {
for (int k = j ; k > i ; k--) {swap(a[k] , a[k-1]); ops++; }
break;
}
}
//cout << a << '\n';
}
ans = min(ans , ops);
return;
}
if (a.back() != 'G' && x > 0) brute(i+1 , x-1 , y , z , a + 'G');
if (a.back() != 'R' && y > 0) brute(i+1 , x , y-1 , z , a + 'R');
if (a.back() != 'Y' && z > 0) brute(i+1 , x , y , z - 1 , a + 'Y');
}
signed main(){
cin >> n >> b;
int x = 0 ,y = 0 ,z =0 ;
for (auto i : b) {if (i == 'G') x++; else if (i == 'R') y++; else z++;}
brute(0 , x , y , z , "");
cout << (ans == 1e9 ? -1 : ans);
}
# | 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... |