# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1051587 | Tob | 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 <bits/stdc++.h>
#inlcude "shoes.h"
#define F first
#define S second
using namespace std;
typedef long long ll;
const int N = 2e5 + 7;
int fen[N];
stack <int> st[N];
void add(int x, int val) {for (x++; x < N; x += x & -x) fen[x] += val;}
int get(int x) {int out = 0; for (x++; x; x -= x & -x) out += fen[x]; return out;}
ll count_swaps(vector <int> a) {
int n = a.size();
vector <int> pa(2*n, -1);
for (int i = 0; i < n; i++) {
int x = abs(a[i]);
if (st[x].empty()) st[x].push(i);
else {
if ((a[i] > 0) == (a[st[x].top()] > 0)) st[x].push(i);
else {
pa[st[x].top()] = i;
st[x].pop();
}
}
}
ll res = 0;
for (int i = 0; i < 2*n; i++) {
if (pa[i] == -1) continue;
res += pa[i]-i-(a[i] < 0) - get(pa[i])+get(i);
add(pa[i], 1);
}
return res;
}