# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
433058 | timmyfeng | Horses (IOI15_horses) | C++17 | 0 ms | 0 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>
using namespace std;
#include "sorting.h"
const int N = 200000;
int perm[N], inv[N], first[N], second[N];
bool visited[N];
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int low = 0, high = M + 1;
while (low < high) {
int mid = (low + high) / 2;
copy(S, S + N, perm);
for (int i = 0; i < mid; ++i) {
swap(perm[X[i]], perm[Y[i]]);
}
int count = 0;
fill(visited, visited + N, false);
fill(first, first + N, 0);
fill(second, second + N, 0);
for (int i = 0; i < N; ++i) {
while (perm[i] != i) {
first[count] = perm[i], second[count] = perm[perm[i]], ++count;
swap(perm[i], perm[perm[i]]);
}
}
if (count <= mid) {
high = mid;
copy(S, S + N, perm);
for (int i = 0; i < N; ++i) {
inv[perm[i]] = i;
}
for (int i = 0; i < mid; ++i) {
swap(perm[X[i]], perm[Y[i]]);
swap(inv[perm[X[i]]], inv[perm[Y[i]]]);
P[i] = first[i], Q[i] = second[i];
swap(inv[P[i]], inv[Q[i]]);
P[i] = inv[P[i]], Q[i] = inv[Q[i]];
swap(perm[P[i]], perm[Q[i]]);
}
} else {
low = mid + 1;
}
}
return low;
}