#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;
int a[n];
int cnt0 = 0, cnt1 = 0, cnt2 = 0;
for(int i=0; i<n; i++){
if(s[i] == 'R') a[i] = 0, cnt0++;
else if(s[i] == 'G') a[i] = 1, cnt1++;
else a[i] = 2, cnt2++;
}
if(abs(cnt1-cnt0) > 1){
cout<<-1;
return;
}
int ans = 1e18;
if(cnt1 >= cnt0){
int b[n];
for(int i=0; i<n; i++) b[i] = a[i];
int subans = 0;
for(int i=0; i<n; i++){
int x = (i % 2 ? 0 : 1);
if(b[i] == x) continue;
for(int j=i+1; j<n; j++){
if(b[i] != x) continue;
for(int k=j; k>i; k--){
swap(b[k], b[k-1]);
subans++;
}
}
}
ans = subans;
}
if(cnt0 >= cnt1){
int b[n];
for(int i=0; i<n; i++) b[i] = a[i];
int subans = 0;
for(int i=0; i<n; i++){
int x = (i % 2 ? 1 : 0);
if(b[i] == x) continue;
for(int j=i+1; j<n; j++){
if(b[j] != x) continue;
for(int k=j; k>i; k--){
swap(b[k], b[k-1]);
subans++;
}
break;
}
}
ans = min(subans, ans);
}
cout<<ans<<nl;
}
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 |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |