# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
718856 | Yell0 | Arranging Shoes (IOI19_shoes) | C++17 | 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;
const int MN=1e5+2;
typedef long long ll;
struct bit {
vector<int> arr;
bit(int sz) {
arr=vector<int>(sz,0);
}
void upd(int i,int v) {
for(;i<arr.size();i+=-i&i) arr[i]+=v;
}
int qry(int i) {
int ret=0;
for(;i>0;i-=-i&i) ret+=arr[i];
return ret;
}
};
ll count_swaps(vector<int> S) {
ll N=S.size()/2,ans=0,sub=0;
bit bit(N*2+2);
vector<int> sti(N*2+2,0),edi(N*2+2,0);
S.insert(S.begin(),0);
vector<vector<int>> pos(2*MN);
for(int i=1;i<=2*N;++i) pos[S[i]+N].push_back(i);
for(int i=0;i<N;++i)
for(int j=0;j<pos[i].size();++j) {
ans+=(pos[N+N-i][j]<pos[i][j]);
int f=min(pos[N+N-i][j],pos[i][j]),s=max(pos[N+N-i][j],pos[i][j]);
sti[s]=f;
edi[f]=s;
}
for(int i=1;i<=2*N;++i) {
if(sti[i]) {
bit.upd(sti[i],-1);
if(sti[i]==i-1) continue;
ans+=i-sti[i]-1;
sub+=bit.qry(i)-bit.qry(sti[i]);
} else bit.upd(i,1);
}
return ans-sub/2;
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);
int N;
cin>>N;
vector<int> v(2*N);
for(int i=0;i<2*N;++i) cin>>v[i];
cout<<count_swaps(v)<<'\n';
return 0;
}