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>
#define sz(x) (int)x.size()
using namespace std;
struct fenwick {
int n;
vector<int> t;
fenwick (int n) {
this->n = n;
t.resize(n);
}
void upd(int i, int x) {
while (i < n) {
t[i] += x;
i |= i + 1;
}
}
void upd(int l, int r, int x) {
upd(l, x);
upd(r + 1, -x);
}
int get(int i) {
int r = 0;
for (; i >= 0; i = (i & (i + 1)) - 1) {
r += t[i];
}
return r;
}
};
long long count_swaps(vector<int> s) {
int n = sz(s) / 2;
long long swaps = 0;
set<int> unused;
vector<set<int>> pos(2 * n + 2);
for (int i = 0; i < 2 * n; i++) {
unused.insert(i);
pos[s[i] + n].insert(i);
}
fenwick f(2 * n + 2);
for (int i = 0; i < n; i++) {
int ft = *unused.begin();
unused.erase(unused.begin());
pos[s[ft] + n].erase(ft);
int sc = *pos[-s[ft] + n].begin();
pos[-s[ft] + n].erase(pos[-s[ft] + n].begin());
unused.erase(sc);
int actual_ft = ft + f.get(ft);
int actual_sc = sc + f.get(sc);
if (s[ft] < 0) {
swaps += actual_sc - actual_ft - 1;
f.upd(ft + 1, sc, +1);
} else {
swaps += actual_sc - actual_ft;
f.upd(ft, sc, +1);
}
}
return swaps;
}
/*
g++ -std=gnu++14 -O2 -Wall -pipe -static -o "shoes" "grader.cpp" "shoes.cpp"
*/
# | 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... |