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;
typedef long long ll;
ll count_swaps(vector<int> a) {
int n = a.size() / 2;
ll ans = 0;
map<int, vector<int>> last;
vector<int> pr(2 * n, -1);
for (int i = 2 * n - 1; i >= 0; i--) {
last[a[i]].push_back(i);
}
for (int i = 0; i < 2 * n; i++) {
if (pr[i] != -1)
continue;
pr[i] = last[-a[i]].back();;
last[-a[i]].pop_back();
last[a[i]].pop_back();
ans += abs(pr[i] - i) - 1;
if ((pr[i] > i) != (a[pr[i]] > a[i])) {
ans++;
}
pr[pr[i]] = i;
}
for (int i = 0; i < 2 * n; i++) {
if (pr[i] > i) {
for (int j = i + 1; j < pr[i]; j++) {
if (pr[j] > pr[i]) {
ans--;
}
}
}
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |