# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
313011 | hackermub | 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>
#include "shoes.h"
using namespace std;
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define uid uniform_int_distribution<int>
//mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9+7;
const int64_t INF = 1e18;
const int MAXN = 2e5+7;
int N;
vector<int> tree(MAXN);
void update(int idx,int val=1){
while(idx<=N){
tree[idx]+=val;
idx+=idx&(-idx);
}
}
int read(int idx){
int sum=0;
while(idx>0){
sum+=tree[idx];
idx-=idx&(-idx);
}
return sum;
}
int query(int L,int R){
return read(R)-read(L-1);
}
long long count_swaps(std::vector<int> s) {
N=s.size();
int n=N/2;
queue<int> idx[2*n+1];
for(int i=0;i<2*n;i++){
//cout<<s[i]+n<<" ";
idx[s[i]+n].push(i+1);
}
vector<int> perm;
vector<bool> vis(2*n+1);
for(int i=0;i<2*n;i++)if(!vis[i]){
if(s[i]<0){
perm.pb(i+1); vis[i]=1;
perm.pb(idx[-s[i]+n].front()); idx[-s[i]+n].pop();
vis[perm.back()-1]=1;
}else{
perm.pb(idx[-s[i]+n].front()); idx[-s[i]+n].pop();
vis[perm.back()-1]=1;
perm.pb(i+1); vis[i]=1;
}
idx[s[i]+n].pop();
}
int ans=0;
for(auto x:perm){
ans+=query(x+1,N);
update(x);
}
return ans;
}
#ifdef OFFLINE
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie();
//If you hack my code , You are gay
int n;
cin>>n;
vector<int> s(2*n);
for(int i=0;i<2*n;i++){
cin>>s[i];
}
cout<<count_swaps(s);
return 0;
}
#endif
#undef int