# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
309543 | lucaperju | 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>
using namespace std;
long long fv[100005],ok[100005];
vector<long long>v[100005];
void next (long long &j)
{
++j;
while(ok[j])
++j;
}
struct ura
{
long long sum,val;
}vc[100005];
bool cmp (ura a, ura b)
{
return a.sum<b.sum;
}
long long bc[100005];
long long aib[200005];
long long lsb (long long x)
{
return x&-x;
}
void update (long long pz)
{
for(long long i=pz;i<=200002;i+=lsb(i))
++aib[i];
}
long long query (long long pz)
{
long long rz=0;
for(long long i=pz;i>0;i-=lsb(i))
rz+=aib[i];
return rz;
}
long long count_swaps(std::vector<long long> s) {
long long n=s.size()/2;
long long i,j,mx=0;
for(i=0;i<s.size();++i)
{
if(s[i]>0)
++fv[s[i]],ok[s[i]]=1;
}
j=0;
next(j);
for(i=0;i<s.size();++i)
{
if(s[i]<0)
continue;
if(fv[s[i]]>1)
{
--fv[s[i]];
v[s[i]].push_back(j);
s[i]=j;
next(j);
}
else
fv[s[i]]=0;
}
for(i=0;i<s.size();++i)
if(s[i]<0 && fv[-s[i]]<v[-s[i]].size())
s[i]=-v[-s[i]][fv[-s[i]]++];
// for(i=0;i<s.size();++i)
// cout<<s[i]<<' ';
// cout<<'\n';
for(i=1;i<=n;++i)
fv[i]=0;
long long rz=0;
for(i=0;i<s.size();++i)
{
long long vlc=s[i];
if(vlc<0)
vlc=-vlc;
vc[vlc].sum+=i;
vc[vlc].val=vlc;
}
sort(vc+1,vc+n+1,cmp);
for(i=1;i<=n;++i)
{
bc[vc[i].val]=i;
}
for(i=0;i<s.size();++i)
{
if(s[i]>0)
s[i]=bc[s[i]];
else
s[i]=-bc[-s[i]];
if(s[i]<0)
s[i]=2*(-s[i]-1)+1;
else
s[i]=2*(s[i]-1)+2;
}
// for(i=0;i<s.size();++i)
// cout<<s[i]<<' ';
// cout<<'\n';
for(i=s.size()-1;i>=0;--i)
{
rz+=query(s[i]);
update(s[i]);
}
return rz;
}
/*long long main()
{
long long n,i;
cin>>n;
vector<long long>idk;
for(i=1;i<=n*2;++i)
{
long long a;
cin>>a;
idk.push_back(a);
}
cout<<count_swaps(idk);
}*/