# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
494938 | luka1234 | 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<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
using namespace std;
ll maxn=200001;
ll f[200001];
ll getsum(ll v){
ll ans=0;
for(ll i=v;i>=1;i=(i&(i-1))){
ans+=f[i];
}
return ans;
}
void update(ll v,ll x){
for(ll i=v;i<=maxn;i=2*i-(i&(i-1))){
f[i]+=x;
}
}
long long count_swaps(std::vector<ll> s) {
ll n=s.size()/2;
ll ans=0;
vector<pair<ll,ll>> v;
vector<pair<ll,ll>> inde[maxn];
for(ll i=0;i<s.size();i++){
inde[abs(s[i])].push_back({s[i],i});
}
for(ll i=1;i<=n;i++){
sort(inde[i].begin(),inde[i].end());
for(ll j=0;j<(inde[i].size()/2);j++){
ll l=inde[i][j].ss;
ll r=inde[i][j+(inde[i].size()/2)].ss;
if(l>r){
swap(l,r);
ans++;
}
v.push_back({l+1,r+1});
}
}
for(ll i=1;i<=2*n;i++)
update(i,1);
sort(v.begin(),v.end());
for(pair<ll,ll> i:v){
ans+=getsum(i.ss-1)-getsum(i.ff);
update(i.ff,-1);
update(i.ss,-1);
}
return ans;
}