이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |