#include <bits/stdc++.h>
#define ll long long
#define rep(a,b,c) for(ll a=b; a<c; a++)
#define repr(a,b,c) for(ll a=b-1; a>c-1; a--)
using namespace std;
int n;
string s, s2;
int mn=1e9;
void solve(int j, int r, int y, int g, int l){
if(y<0 || g<0 || r<0) return;
if(!l) s2[j-1]='R';
else if(l&1) s2[j-1]='Y';
else s2[j-1]='G';
if(j==n){
int sum=0;
string s3=s;
rep(i,0,n){
if(s2[i]==s3[i]) continue;
rep(j,i,n){
if(s3[j]==s2[i]){
repr(k,j,i) swap(s3[k+1],s3[k]);
sum+=(j-i);
break;
}
}
}
mn=min(mn,sum);
return;
}
if(!l) solve(j+1,r,y-1,g,1), solve(j+1,r,y,g-1,2);
else if(l&1) solve(j+1,r-1,y,g,0), solve(j+1,r,y,g-1,2);
else solve(j+1,r,y-1,g,1), solve(j+1,r-1,y,g,0);
}
int main(){
cin>>n>>s;
int r=0, g=0, y=0;
rep(i,0,n){
if(s[i]=='R') r++;
if(s[i]=='G') g++;
if(s[i]=='Y') y++;
}
s2=s;
solve(1,r-1,y,g,0);
solve(1,r,y-1,g,1);
solve(1,r,y,g-1,1);
if(mn<1e9) cout<<mn<<endl;
else cout<<-1<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |