# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
707037 | marvinthang | Sorting (IOI15_sorting) | C++17 | 197 ms | 12476 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 "sorting.h"
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
auto check = [&] (int r, bool trace = false) {
vector <int> perm(S, S + N);
REP(i, r) swap(perm[X[i]], perm[Y[i]]);
vector <int> pos(N);
REP(i, N) pos[perm[i]] = i;
vector <pair <int, int>> swaps;
auto __swap = [&] (int i, int j) {
swap(perm[i], perm[j]);
swap(pos[perm[i]], pos[perm[j]]);
};
REP(i, N) {
if (perm[i] != i) {
swaps.push_back(make_pair(perm[i], i));
__swap(i, pos[i]);
}
}
if (!trace) return swaps.size() <= r;
while (swaps.size() < r) swaps.push_back(make_pair(0, 0));
copy(S, S + N, perm.begin());
REP(i, N) pos[perm[i]] = i;
REP(i, r) {
__swap(X[i], Y[i]);
P[i] = pos[swaps[i].fi];
Q[i] = pos[swaps[i].se];
__swap(P[i], Q[i]);
}
return true;
};
int l = 0, r = N;
while (l < r) {
int m = l + r >> 1;
if (check(m)) r = m;
else l = m + 1;
}
check(l, true);
return l;
}
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... |