# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
850059 | Bach21 | 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 <bits/stdc++.h>
#include "shoes.h"
using namespace std;
const int nmax=2e5+5;
int BIT[nmax];
void update(int s,int val)
{
for (int i=s;i<nmax;i+=i&-i)
{
BIT[i]+=val;
}
}
int query(int s)
{
int res=0;
for (int i=s;i;i-=i&(-i))
{
res+=BIT[i];
}
return res;
}
int64_t count_swaps(vector<int> s)
{
int64_t res=0;
int n=s.size()/2;
vector<pair<int,int>> v[nmax];
for (int i=0;i<s.size();i++)
{
v[abs(s[i])].push_back({s[i],i});
}
vector<pair<int,int>> xl;
for (int i=0;i<n;i++)
{
sort(v[i].begin(),v[i].end());
for (int j=0;j<(v[i].size()/2);j++)
{
int l=v[i][j].second;
int r=v[i][j+(v[i].size()/2)].second;
if (l>r)
{
swap(l,r);
res++;
}
xl.push_back({l+1,r+1});
}
}
for (int i=1;i<=n*2;i++)
{
update(i,1);
}
sort(xl.begin(),xl.end());
for (auto x : xl)
{
res+=query(x.second-1)- query(x.first);
update(x.first,-1);
update(x.second,-1);
}
return res;
}