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>
using namespace std;
int sonuc=0;
int sonuc2=0;
int main() {
int n;
string s;
cin >> n >> s;
string bas=s;
int r=0,g=0;
for (int i = 0; i < n; i++)
{
if(s[i]=='R')
r++;
else
g++;
}
if(abs(r-g)>1){
cout << "-1";
return 0;
}
for (int i = 0; i < n; i++)
{
if((!(i % 2)) and s[i]=='R' ){
for (int j = i+1; j < n; j++){
if(s[j]=='G'){
swap(s[j],s[i]);
sonuc+=j-i;
break;
}
}
}
if((i % 2) and s[i]=='G' ){
for (int j = i+1; j < n; j++){
if(s[j]=='R'){
swap(s[j],s[i]);
sonuc+=j-i;
break;
}
}
}
}
s=bas;
for (int i = 0; i < n; i++)
{
if((!(i % 2)) and s[i]=='G' ){
for (int j = i+1; j < n; j++){
// cout << s[i]<< i <<s[j]<< j <<endl;
if(s[j]=='R'){
swap(s[j],s[i]);
sonuc2+=j-i;
break;
}
}
}
if((i % 2) and s[i]=='R' ){
for (int j = i+1; j < n; j++){
// cout << s[i]<< i <<s[j]<< j <<endl;
if(s[j]=='G'){
swap(s[j],s[i]);
sonuc2+=j-i;
break;
}
}
}
}
//cout <<sonuc << sonuc2 <<endl;
cout << min(sonuc,sonuc2);
}
# | 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... |