This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include "shoes.h"
using namespace std;
#define ve vector
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9 + 10;
struct SegmentTree {
int Cap;
ve<int> Seg;
int left(int x) { return 2 * x; }
int right(int x) { return 2 * x + 1; }
int parent(int x) { return x / 2; }
void build(int a) {
Cap = 1 << (int)ceil(log2(a));
Seg = ve<int>(2 * Cap);
}
int query(int a, int b) {
return query(a, b, 0, Cap - 1, 1);
}
int query(int a, int b, int i, int j, int curr) {
if (a <= i && j <= b) return Seg[curr];
if (b < i || j < a) return 0;
int m = (i + j) / 2;
return query(a, b, i, m, left(curr)) + query(a, b, m + 1, j, right(curr));
}
void update(int p) {
Seg[Cap + p] = 1;
int curr = parent(Cap + p);
while (curr != 0) {
Seg[curr] = Seg[left(curr)] + Seg[right(curr)];
curr = parent(curr);
}
}
};
ll count_swaps(ve<int> S) {
int n;
ve<int> A;
ve<pii> P;
map<int, ve<int>> M;
SegmentTree Seg;
n = (int)S.size() / 2;
for (int i = 0; i < 2 * n; i++) {
int s = S[i];
if (s < 0) P.push_back({i, -s});
else M[s].push_back(i);
}
int ans = INF;
do {
A = ve<int>(2 * n);
map<int, int> X;
for (int i = 0; i < n; i++) {
int s = P[i].second;
int j = X[s]++;
A[P[i].first] = 2 * i;
A[M[s][j]] = 2 * i + 1;
}
Seg.build(2 * n);
int a = 0;
for (int i = 0; i < 2 * n; i++) {
a += Seg.query(A[i], 2 * n - 1);
Seg.update(A[i]);
}
ans = min(ans, a);
}
while (next_permutation(P.begin(), P.end()));
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... |