# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
151924 | ivandasfs | 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 <iostream>
#include <cstdio>
#include <set>
using namespace std;
typedef long long ll;
const int OFF = 1e5;
int off = 1;
set<int> s[200005];
int tur[600005];
//int a[200005];
void update(int node, int val) {
if (!node) return ;
if (node>=off) {
tur[node] = val;
}else {
tur[node] = tur[node*2] + tur[node*2+1];
}
update(node/2, val);
}
int query(int l, int r, int x, int y, int node) {
if (x>r or y<l) return 0;
if (l>=x and r<=y) return tur[node];
return query(l, (l+r)/2, x, y, node*2)+query((l+r)/2+1, r, x, y, node*2+1);
}
ll count_swaps(int *a) {
int n = sizeof(a)/sizeof(a[0]);
while (off<n) off*=2;
for (int i=0 ; i<n ; i++) {
update(off+i, 1);
s[a[i]+OFF].insert(i);
}
ll sol=0;
for (int i=0 ; i<n ; i++) {
if (!a[i]) continue;
//cout <<a[i]<<endl;
int pos=*s[-a[i]+OFF].begin();
//cout <<i<<":"<<pos<<endl;
int g=query(0, off-1, i, pos, 1)-2;
if (a[i]>0) g++;
sol+=g;
//cout <<"sol="<<sol<<endl;
update(pos+off, 0);
s[a[i]+OFF].erase(s[a[i]+OFF].begin());
s[-a[i]+OFF].erase(s[-a[i]+OFF].begin());
a[pos]=0;
}
return sol;
}