//#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
#define pr rr
int lowbit(int x){return x&-x;}
int bit[500005];
void upd(int pl,int v){
while(pl<=500000){
bit[pl]+=v;
pl+=lowbit(pl);
}
}
int qsum(int x){
int ret=0;
while(x){
ret+=bit[x];
x-=lowbit(x);
}
return ret;
}
vector<int>rr;
vector<int>gr;
vector<int>yr;
int n;
string s;
signed main(){
cin>>n>>s;
s="*"+s;
for(int i=1;i<=n;i++){
if(s[i]=='R'){
pr.push_back(i);
}
if(s[i]=='G'){
gr.push_back(i);
}
}
if(pr.size()>=gr.size()+2){
cout<<-1<<endl;
return 0;
}
if(gr.size()>=pr.size()+2){
cout<<-1<<endl;
return 0;
}
int fans=1e16;
if(gr.size()>=pr.size()){
int ans=0;
for(int i=1;i<=n;i++){
if(i%2==1){
int pl=gr[(i+1)/2-1];
ans+=pl-i+qsum(n)-qsum(pl);
upd(pl,1);
}else{
int pl=pr[i/2-1];
ans+=pl-i+qsum(n)-qsum(pl);
upd(pl,1);
}
}
fans=ans;
}
for(int i=1;i<=500000;i++){bit[i]=0;}
if(gr.size()<=pr.size()){
int ans=0;
for(int i=1;i<=n;i++){
// cout<<i<<endl;
if(i%2==0){
int pl=gr[(i+1)/2-1];
ans+=pl-i+qsum(n)-qsum(pl);
upd(pl,1);
}else{
int pl=pr[(i+1)/2-1];
ans+=pl-i+qsum(n)-qsum(pl);
upd(pl,1);
}
}
fans=min(fans,ans);
}
cout<<fans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Runtime error |
6 ms |
8276 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Runtime error |
6 ms |
8276 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4180 KB |
Output is correct |
2 |
Correct |
3 ms |
4180 KB |
Output is correct |
3 |
Correct |
2 ms |
4180 KB |
Output is correct |
4 |
Correct |
2 ms |
4180 KB |
Output is correct |
5 |
Correct |
3 ms |
4180 KB |
Output is correct |
6 |
Correct |
2 ms |
4180 KB |
Output is correct |
7 |
Correct |
2 ms |
4180 KB |
Output is correct |
8 |
Correct |
2 ms |
4180 KB |
Output is correct |
9 |
Correct |
2 ms |
4180 KB |
Output is correct |
10 |
Correct |
3 ms |
4180 KB |
Output is correct |
11 |
Correct |
2 ms |
4180 KB |
Output is correct |
12 |
Correct |
2 ms |
4180 KB |
Output is correct |
13 |
Correct |
2 ms |
4180 KB |
Output is correct |
14 |
Correct |
2 ms |
4180 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4180 KB |
Output is correct |
2 |
Runtime error |
6 ms |
8276 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |