# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
852301 | teo_thrash | 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.cpp"
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn=2e5+5;
int bit[maxn];
void update(int idx, int delta){
for(; idx<maxn; idx += (idx & -idx)){
bit[idx]+=delta;
}
}
ll query(int idx){
ll sum=0;
for(; idx>0; idx -= (idx & -idx)){
sum+=bit[idx];
}
return sum;
}
ll query(int l, int r){
return query(r)-query(l-1);
}
ll count_swaps( vector<int> a){
ll ans;
vector<pii> shoes[maxn];
int n=a.size();
for(int i=0; i<n; i++){
shoes[abs(a[i])].pb({a[i], i});
}
vector<pii> pairs;
for(int i=1; i<=a.size()/2; i++){
sort(shoes[i].begin(), shoes[i].end());
for(int j=0; j<shoes[i].size()/2; j++){
int l=shoes[i][j].second;
int r=shoes[i][j + (shoes[i].size()/2) ].second;
if(l>r){
swap(l, r);
ans++;
}
pairs.pb({l, r});
}
}
for(int i=1; i<=a.size(); i++){
update(i, 1);
}
sort(pairs.begin(), pairs.end());
for(auto p: pairs){
ans+=query(p.first, p.second);
update(p.first, -1);
update(p.second, -1);
}
return ans;
}