이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/**
* author: tourist
* created:
**/
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "algo/debug.h"
#else
#define debug(...) 42
#endif
template <typename T>
class fenwick {
public:
vector<T> fenw;
int n;
fenwick(int _n) : n(_n) {
fenw.resize(n);
}
void modify(int x, T v) {
while (x < n) {
fenw[x] += v;
x |= (x + 1);
}
}
T get(int x) {
T v{};
while (x >= 0) {
v += fenw[x];
x = (x & (x + 1)) - 1;
}
return v;
}
};
long long count_swaps(vector<int> a) {
int n = a.size() / 2;
long long swaps = 0;
fenwick<int> fenw(2 * n);
map<int, deque<int>> mp;
for (int i = 0; i < 2 * n; i++) {
mp[a[i]].push_back(i);
}
for (int i = 0; i < 2 * n; i++) {
fenw.modify(i, 1);
}
set<int> unlock;
for (int i = 0; i < 2 * n; i++) {
unlock.insert(i);
}
for (int i = 0; i < 2 * n; i += 2) {
int p = mp[-a[*unlock.begin()]].front();
swaps += fenw.get(p - 1) + (a[*unlock.begin()] > 0) - 1;
fenw.modify(*unlock.begin(), -1);
fenw.modify(p, -1);
mp[a[*unlock.begin()]].pop_front();
mp[-a[*unlock.begin()]].pop_front();
unlock.erase(*unlock.begin());
unlock.erase(p);
}
return swaps;
}
# | 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... |