#include<bits/stdc++.h>
using namespace std;
#define lalala ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
#define ll long long
#define pb push_back
#define N 200005
int n;
int arr[N];
int main(){
lalala;
cin>>n;
string str;cin>>str;
int r=0,g=0;
for(int i=0;i<n;i++){
if(str[i]=='G'){
arr[i]=1;g++;}
if(str[i]=='R'){
arr[i]=2;r++;}
}
if(abs(g-r)>1){
cout<<-1<<endl;
return 0;
}
ll int cev=0;
for(int i=0;i<n-1;i++){
//cout<<cev<<endl;
if(arr[i]==arr[i+1]){
for(int j=n-1;j>i;j--){
if(arr[j]==arr[j-1]&&arr[j]!=arr[i]){
if(abs(g-r==0)){
swap(arr[i+1],arr[j-1]);
//cout<<i<< " "<<j-1<<endl;
cev+=(j-1-(i+1));break;
}
else{
swap(arr[i+1],arr[j]);
// cout<<i<< " "<<j<<endl;
cev+=(j-(i+1));break;
}
}
}
}
}cout<<cev<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |