# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
704095 | 1075508020060209tc | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++14 | 6 ms | 8276 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#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];
int 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;
}
Compilation message (stderr)
# | 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... |