# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
158574 | phungha | 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>
using namespace std;
const int MAXN = 100001;
int n, BIT[200001];
vector<pair<int, int> > ord[100001], a;
long long ans = 0;
void update(int i, int v) {
while (i <= 200000) {
BIT[i] += v;
i += i & (-i);
}
}
int sum(int i) {
int s = 0;
while (i > 0) {
s += BIT[i];
i -= i & -i;
}
return s;
}
int main() {
//freopen("test117.in", "r", stdin);
//freopen("test01.out", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= 2*n; i++) {
int s;
scanf("%d", &s);
ord[abs(s)].push_back({s, i});
}
for (int i = 1; i <= n; i++) {
sort(ord[i].begin(), ord[i].end());
for (int j = 0; j < ord[i].size()/2; j++) {
int l = ord[i][j].second;
int r = ord[i][j + ord[i].size()/2].second;
if (l > r) {
swap(l, r);
ans++;
}
a.push_back({l, r});
}
}
sort(a.begin(), a.end());
for (int i = 1; i <= 2*n; i++)
update(i, 1);
for (int i = 0; i < a.size(); i++) {
ans += sum(a[i].second-1) - sum(a[i].first);
update(a[i].first, -1);
update(a[i].second, -1);
}
printf("%lld\n", ans);
}