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;
#define REP(i, n) for(int i = 0; i < n; i++)
#include "sorting.h"
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
auto check = [&](int R) {
vector<int> s(N), ws(N), wf(N);
REP(i, N) s[i] = S[i];
auto f = s;
REP(i, R) swap(f[X[i]], f[Y[i]]);
REP(i, N) {
ws[S[i]] = i;
wf[f[i]] = i;
}
int j = 0;
REP(i, R) {
int &p = s[X[i]], &q = s[Y[i]];
swap(p, q);
swap(ws[p], ws[q]);
while(j < N && j == f[j]) j++;
if(j < N) {
int &a = ws[j], &b = ws[f[j]], &x = wf[j], &y = wf[f[j]];
P[i] = a, Q[i] = b;
swap(a, b), swap(s[a], s[b]);
swap(x, y), swap(f[x], f[y]);
}
else P[i] = Q[i] = 0;
}
REP(i, N) if(i != s[i]) return false;
return true;
};
int l = 0, r = M;
while(l < r) {
int m = (l + r) / 2;
if(check(m))
r = m;
else
l = m + 1;
}
check(l);
return l;
}
# | 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... |