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;
const int MAXN = 2e5+5;
typedef long long ll;
int bit[MAXN];
int nxt[MAXN];
int n;
void upd(int p, int v) {
for (; p < n; p |= (p+1)) bit[p] += v;
}
int sum(int p) {
int s = 0;
for (p++; p > 0; p &= (p-1)) s += bit[p-1];
return s;
}
queue<int> occ[MAXN];
bool matched[MAXN];
int _abs(int x) {
return x < 0 ? -x : x;
}
ll count_swaps(vector<int> s) {
n = s.size();
for (int i = 0; i < n; i++) {
int x = _abs(s[i]);
if (occ[x].size() && s[occ[x].front()] != s[i]) {
nxt[occ[x].front()] = i;
occ[x].pop();
}
else occ[x].push(i);
}
for (int i = 1; i <= n/2; i++) assert(occ[i].empty());
ll ans = 0;
for (int i = 0; i < n; i++) {
if (matched[i]) continue;
int dist = nxt[i]+sum(nxt[i])-(s[i] < 0);
ans += dist;
upd(i, -1);
upd(nxt[i], -1);
matched[i] = matched[nxt[i]] = 1;
}
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... |