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 <random>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
mt19937 mt(2009190008);
int rand_rng(int l, int r) {
uniform_int_distribution<int> p(l, r - 1);
return p(mt);
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
fill(P, P + M, 0);
fill(Q, Q + M, 0);
for(int i = 0; i < M; ++i) {
if(is_sorted(S, S + N)) {
return i;
}
swap(S[X[i]], S[Y[i]]);
if(is_sorted(S, S + N)) {
return i + 1;
}
vector<int> col(N, -1);
for(int j = 0; j < N; ++j) {
if(col[j] != -1) continue;
int pos = j;
while(col[pos] == -1) {
col[pos] = j;
pos = S[pos];
}
}
do {
P[i] = rand_rng(0, N);
Q[i] = rand_rng(0, N);
} while(P[i] == Q[i] || col[P[i]] != col[Q[i]]);
swap(S[P[i]], S[Q[i]]);
}
if(is_sorted(S, S + N)) {
return M;
}
return -1;
}
# | 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... |