이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |