# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
306018 | kylych03 | 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>
#include "grader.cpp"
using namespace std;
long long res[200010];
int vis[200011], dp[200010];
vector <int> a1[100002], b[100002];
const int maxn=2e3+7;
vector<int> a;
vector<int> t(4*maxn);
vector<int> add(4*maxn,0);
void push(int v)
{
if(add[v])
{
add[v*2]+=add[v];
add[v*2+1]+=add[v];
add[v]=0;
}
}
void build(int v,int tl,int tr)
{
if(tl==tr)
{
t[v]=a[tl];
}
else
{
int tm=(tl+tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
}
}
int get(int v,int tl,int tr,int pos)
{
if(tl==tr)
{
t[v]+=add[v];
add[v]=0;
return t[v];
}
else
{
int tm=(tl+tr)/2;
push(v);
if(pos<=tm)
{
return get(v*2,tl,tm,pos);
}
else
{
return get(v*2+1,tm+1,tr,pos);
}
}
}
void Add(int v,int tl,int tr,int l,int r,int val)
{
if(l>r)
{
return;
}
if(tl==l && tr==r)
{
add[v]+=val;
return;
}
else
{
int tm=(tl+tr)/2;
Add(v*2,tl,tm,l,min(r,tm),val);
Add(v*2+1,tm+1,tr,max(tm+1,l),r,val);
}
}
long long count_swaps(std::vector<int> s) {
long long sum = 0;
for(int i = 0 ; i < s.size(); i++){
if(s[i]>0)
a1[s[i]].push_back(i);
else
b[abs(s[i])].push_back(i);
}
for(int i = 0 ; i <100001;i++){
for(int j = 0 ; j < a1[i].size();j++){
dp[a1[i][j]]=b[i][j];
dp[b[i][j]]=a1[i][j];
}
}
int n = s.size()+1;
for(int i = 0 ; i < s.size(); i++){
if(vis[i]==1)
continue;
int j=i+1;
sum += dp[i] - i - 1 + get(1,0,n-1,dp[i]) - get(1,0,n-1,i+1);
Add(1,0,n,i,dp[i],1);
cout << sum<<endl;
if(s[i] > 0)
sum++;
vis[dp[i]]=1;
}
return sum;
}