# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
316127 | nikatamliani | Sorting (IOI15_sorting) | C++14 | 340 ms | 20088 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 < int > _Q(N), _P(N), _S(N), id(N);
int l = 0, r = M - 1, ans = -1;
while(r >= l) {
auto SWAP = [&](int i, int j) {
swap(id[_S[i]], id[_S[j]]);
swap(_S[i], _S[j]);
};
for(int i = 0; i < N; ++i) _S[i] = S[i];
int m = l + r >> 1;
int swaps = -1;
for(int i = 0; i < m; ++i) {
swap(_S[X[i]], _S[Y[i]]);
}
for(int i = 0; i < N; ++i) {
id[_S[i]] = i;
}
for(int i = 0; i < N; ++i) {
if(id[i] == i) continue;
_P[++swaps] = i;
_Q[swaps] = _S[i];
SWAP(id[i], i);
}
if(swaps + 1 <= m) {
for(int i = 0; i < N; ++i) _S[i] = S[i];
for(int i = 0; i < N; ++i) id[_S[i]] = i;
for(int i = 0; i < m; ++i) {
SWAP(X[i], Y[i]);
if(i <= swaps) {
P[i] = id[_P[i]];
Q[i] = id[_Q[i]];
SWAP(id[_P[i]], id[_Q[i]]);
} else {
P[i] = Q[i] = 0;
}
}
r = m - 1;
ans = m;
} else {
l = m + 1;
}
}
return ans;
}
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... |