# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
677047 | hello_there_123 | 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;
#define int long long
int n;
int ft[100005];
inline int ls(int x){return x & -x; }
int qry(int p){
p++;
if(p<1) return 0;
int r = 0;
for(;p;p-=ls(p)) r+=ft[p];
return r;
}
void upd(int p, int v){
p++;
for(;p<=n;p+=ls(p)) ft[p]+=v;
}
long long count_swaps(vector<int>s) {
int ans = 0;
n = s.size();
map<int,queue<int> >mp;
for(int i=0;i<n;i++){
int x = s[i];
mp[x].push(i);
}
bool arr[n+3];
memset(arr,0,sizeof(arr));
for(int i=0;i<n;i++){
mp[s[i]].pop();
if(arr[i]) continue;
int first = mp[-s[i]].front();
mp[-arr[i]].pop();
ans+=first-i-1-(qry(first)-qry(i-1));
upd(first,1);
arr[first] = 1;
if(s[i]>0) ans++;
}
return ans;
}