#include <bits/stdc++.h>
#define int long long
//#define double long double
#define Nanase_Kurumi_aka_menhera_chan_is_mine ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb push_back
#define pi pair<double,int>
#define ALL(i) i.begin(),i.end()
#define gcd(i,j) __gcd(i,j)
#define fi first
#define se second
#define eps 0.00000001
#define ist insert
#define DNE nullptr
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math")
//#pragma GCC optimize("O2")
int max(int x,int y){return x>=y?x:y;}
int min(int x,int y){return x>=y?y:x;}
using namespace std;
typedef int ll;
const int N=100005;
const int M=1000005;
const int MOD=1000000007;//998244353;
const int INF=1000000000000000000;//2147483647;
int n;
string s;
int c[2]{};
inline void sol(){
cin >>n>>s;
vector<int> R(n),G(n);
for (int i=0;i<n;i++){
if (s[i]=='R')
R.pb(i);
else G.pb(i);
}
if (abs((int)R.size()-(int)G.size())>1){
cout <<-1;
return;
}
if (R.size()==G.size()){
string t=s;
int ans=0,cnt=0;
for (int i=0;i<n;i++){
if (i&1){
for (int j=i;j<n;j++)
if (s[j]=='G'){
cnt+=j-i;
for (int k=j;k>i;k--)
swap(s[k],s[k-1]);
break;
}
}
else{
for (int j=i;j<n;j++)
if (s[j]=='R'){
cnt+=j-i;
for (int k=j;k>i;k--)
swap(s[k],s[k-1]);
break;
}
}
}
s=t;ans=cnt;cnt=0;
for (int i=0;i<n;i++){
if (i&1){
for (int j=i;j<n;j++)
if (s[j]=='R'){
cnt+=j-i;
for (int k=j;k>i;k--)
swap(s[k],s[k-1]);
break;
}
}
else{
for (int j=i;j<n;j++)
if (s[j]=='G'){
cnt+=j-i;
for (int k=j;k>i;k--)
swap(s[k],s[k-1]);
break;
}
}
}
cout <<min(ans,cnt)<<'\n';
}
else if (R.size()<G.size()){
int cnt=0;
for (int i=0;i<n;i++){
if (i&1){
for (int j=i;j<n;j++)
if (s[j]=='R'){
cnt+=j-i;
for (int k=j;k>i;k--)
swap(s[k],s[k-1]);
break;
}
}
else{
for (int j=i;j<n;j++)
if (s[j]=='G'){
cnt+=j-i;
for (int k=j;k>i;k--)
swap(s[k],s[k-1]);
break;
}
}
}
cout <<cnt<<'\n';
}
else{
int cnt=0;
for (int i=0;i<n;i++){
if (i&1){
for (int j=i;j<n;j++)
if (s[j]=='G'){
cnt+=j-i;
for (int k=j;k>i;k--)
swap(s[k],s[k-1]);
break;
}
}
else{
for (int j=i;j<n;j++)
if (s[j]=='R'){
cnt+=j-i;
for (int k=j;k>i;k--)
swap(s[k],s[k-1]);
break;
}
}
}
cout <<cnt<<'\n';
}
}
signed main(){
Nanase_Kurumi_aka_menhera_chan_is_mine
int _=1;
//cin >>_;
while (_--) sol();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 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 |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
312 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |