# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
102713 | wxh010910 | Sorting (IOI15_sorting) | C++17 | 224 ms | 18760 KiB |
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 "sorting.h"
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
vector<bool> visit(N);
vector<int> after(N);
auto check = [&](int mid) {
for (int i = 0; i < N; ++i) {
after[i] = S[i];
visit[i] = false;
}
for (int i = 0; i < mid; ++i) {
swap(after[X[i]], after[Y[i]]);
}
int ans = 0;
for (int i = 0; i < N; ++i) {
if (!visit[i]) {
for (int j = i; ; j = after[j]) {
visit[j] = true;
if (!visit[after[j]]) {
P[ans] = i;
Q[ans] = after[j];
++ans;
} else {
break;
}
}
}
}
for (int i = ans; i < mid; ++i) {
P[i] = Q[i] = 0;
}
return ans <= mid;
};
int l = 0, r = N;
while (l < r) {
int mid = (l + r) >> 1;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
check(r);
vector<int> pos(N);
vector<int> to(N);
for (int i = 0; i < N; ++i) {
pos[i] = to[i] = i;
}
for (int i = r - 1; ~i; --i) {
P[i] = to[P[i]];
Q[i] = to[Q[i]];
swap(pos[X[i]], pos[Y[i]]);
to[pos[X[i]]] = X[i];
to[pos[Y[i]]] = Y[i];
}
return r;
}
Compilation message (stderr)
# | 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... |