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"
#include "shoes.h"
using namespace std;
long long count_swaps(std::vector<int> v) {
int n = v.size();
vector <int> t (n + 1, 0);
auto update = [&] (int x, int val) {
for(int i = x; i <= n; i += i & (-i)) {
t[i] += val;
}
};
auto query = [&] (int x) {
long long sum = 0;
for(int i = x; i > 0; i -= i & (-i)) {
sum += t[i];
}
return sum;
};
map <int, queue <int>> pos;
for(int i = 1; i <= n; i++) {
pos[v[i - 1]].push(i);
update(i, 1);
}
long long ans = 0;
for(int i = 1; i <= n; i++) {
if(v[i - 1] == 0) continue;
int idx = pos[-v[i - 1]].front();
pos[-v[i - 1]].pop();
pos[v[i - 1]].pop();
update(i, -1);
update(idx, -1);
ans += query(idx);
if(v[i - 1] > 0) ++ans;
v[i - 1] = v[idx - 1] = 0;
}
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... |