# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
467816 | jazzup | Arranging Shoes (IOI19_shoes) | C++14 | 0 ms | 0 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.
#include "shoes.h"
#include <deque>
int Fenwick[200010];
void add(int a,int b){
for(int i=a;i<=200000;i+=(i&(-i))){
Fenwick[i]+=b;
}
}
int query(int a,int b){
if(a!=0){
return query(0,b)-query(0,a);
}
int ret=0;
for(int i=b;i>0;i-=(i&(-i))){
ret+=Fenwick[i];
}
return ret;
}
std::vector<std::pair<int,int> > p;
std::deque<int> v[200010];
long long count_swaps(std::vector<int> s) {
int n=s.size();
s.insert(s.begin(),0);
for(int i=1;i<=n;i++){
if(!v[n-s[i]].empty()){
p.push_back(std::make_pair(v[n-s[i]].front(),i));
v[n-s[i]].pop_front();
}
else{
v[n+s[i]].push_back(i);
}
}
for(int i=1;i<=n;i++){
add(i,1);
}
// for(int i=0;i<p.size();i++){
// printf("%d %d\n",p[i].first,p[i].second);
// }
long long ans=0;
std::sort(p.begin(),p.end());
for(int i=0;i<p.size();i++){
ans+=query(p[i].first,p[i].second-1);
if(s[p[i].first]>0)ans++;
add(p[i].first,-1);
add(p[i].second,-1);
// printf("%d %d\n",p[i].first,p[i].second);
}
return ans;
}