# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
623576 | Vanio | 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>
using namespace std;
long long n,a[200006],i,t=1,p[200006],j,b[200006],br,f[200006],q;
vector<int> l[200006],r[200006];
void upd(int k, int d){
while(k<=n){
b[k]+=d;
k=k+(k&(-k));
}
}
int rsq(int k){
long long s=0;
while(k>0){
s=s+b[k];
k=k-(k&(-k));
}
return s;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin>>n;
n=n*2;
for(i=1;i<=n;i++){
cin>>a[i];
if(a[i]<0) l[abs(a[i])].push_back(i);
else r[abs(a[i])].push_back(i);
}
for(i=1;i<=n;i++){
if(f[i]==0){
f[i]=1;
q=abs(a[i]);
if(a[i]<0){
f[r[q][0]]=1;
br+=r[q][0]+rsq(r[q][0])-t-1;
upd(i,1);
upd(r[q][0],-1);
l[q].erase(l[q].begin());
r[q].erase(r[q].begin());
}
else{
f[l[q][0]]=1;
br+=l[q][0]+rsq(l[q][0])-t;
upd(i,1);
upd(l[q][0],-1);
r[q].erase(r[q].begin());
l[q].erase(l[q].begin());
}
t+=2;
}
}
cout<<br;
return 0;
}