#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;
}
if(r==g){
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);
return 0;
}
if(r>g){
for (int i = 0; i < n; i++)
{
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;
}
}
}
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;
}
}
}
}
cout << sonuc;
return 0;
}
else
for (int i = 0; i < n; i++)
{
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;
}
}
}
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;
}
}
}
}
cout << sonuc2 << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
292 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |