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>
//#define int long long
//#define double long double
#define Nanase_Kurumi_aka_menhera_chan_is_mine ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define pi pair<double,int>
#define ALL(i) i.begin(),i.end()
#define gcd(i,j) __gcd(i,j)
#define fi first
#define se second
#define eps 0.00000001
#define ist insert
#define DNE nullptr
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
//#pragma GCC optimize("O2")
int max(int x,int y){return x>=y?x:y;}
int min(int x,int y){return x>=y?y:x;}
using namespace std;
typedef int ll;
const int N=4005;
const int M=1000005;
const int MOD=1000000007;//998244353;
const int INF=2147483647;//1000000000000000000;
int n,ans;
int c[3];// R G Y
void dfs(char c,int k,string s,int calc){
if (k==n){
ans=calc;
return;
}
int i;
for (i=k;i<n;i++){
if (s[i]==c){
calc+=i-k;
for (int j=i;j>k;j--)
swap(s[j],s[j-1]);
break;
}
}
//cout <<k<<" "<<s<<'\n';
if (i==n||calc>=ans) return;
if (c=='R'){
dfs('G',k+1,s,calc);
dfs('Y',k+1,s,calc);
}
if (c=='G'){
dfs('R',k+1,s,calc);
dfs('Y',k+1,s,calc);
}
if (c=='Y'){
dfs('R',k+1,s,calc);
dfs('G',k+1,s,calc);
}
}
inline void sol(){
string s;
cin >>n>>s;
ans=INF;
dfs('R',0,s,0);
dfs('G',0,s,0);
dfs('Y',0,s,0);
if (ans==INF) cout <<-1;
else cout <<ans;
}
signed main(){
Nanase_Kurumi_aka_menhera_chan_is_mine
int _=1;
//cin >>_;
while (_--) sol();
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... |