# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
289866 | Bill_00 | 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 <shoes.h>
#include <bits/stdc++.h>
using namespace std;
queue<int> l[200005], r[200005];
long long s[200005];
int a[200005];
int n;
long long tree[1000005];
void add(int i, int x)
{
while(i <= 2 * n)
{
s[i] += x;
i += (i & (-i));
}
}
long long sum(int i)
{
long long res = 0;
while(i > 0)
{
res += s[i];
i -= (i & (-i));
}
return res;
}
long long count_swaps(vector<int> b)
{
n = b.size() / 2;
for(i = 1; i <= 2 * n; i++) a[i] = b[i - 1];
int i, j;
long long ans = 0;
cin >> n;
for(i = 1; i <= 2 * n; i++)
{
cin >> a[i];
if(a[i] > 0) r[a[i]].push(i);
else l[-a[i]].push(i);
}
for(i = 1; i <= 2 * n; i++) add(i, 1);
for(i = 1; i <= 2 * n; i++) if(sum(i) - sum(i - 1)){
if(a[i] < 0)
{
int id;
id = r[-a[i]].front();
ans += sum(id) - sum(i) - 1;
add(id, -1);
l[-a[i]].pop();
r[-a[i]].pop();
}
else {
int id;
id = l[a[i]].front();
ans += sum(id) - sum(i);
add(id, -1);
l[a[i]].pop();
r[a[i]].pop();
}
}
return ans;
}
/*
6
-2 2 2 -2 -2 2
*/