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);
if(is_sorted(S, S + N)) {
return 0;
}
int L = 0;
while(true) {
swap(S[X[L]], S[Y[L]]);
++L;
vector<bool> vis(N, false);
int cycles = 0;
for(int i = 0; i < N; ++i) {
if(vis[i]) continue;
int pos = i;
++cycles;
while(!vis[pos]) {
vis[pos] = true;
pos = S[pos];
}
}
int steps = N - cycles;
if(L >= steps) break;
}
for(int i = 0; i < L; ++i) {
int cycles = 0;
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];
}
++cycles;
}
if(cycles == N) {
P[i] = 0;
Q[i] = 0;
}
else {
vector<vector<int> > G(N);
for(int j = 0; j < N; ++j) {
G[col[j]].push_back(j);
}
for(int j = 0; j < N; ++j) {
if(G[j].size() >= 2) {
P[i] = G[j][0];
Q[i] = G[j][1];
break;
}
}
swap(S[P[i]], S[Q[i]]);
}
}
for(int i = 0; i < L; ++i) {
if(P[i] == Q[i]) continue;
for(int j = L - 1; j >= i + 1; --j) {
if(X[j] == Y[j]) continue;
if(X[j] == P[i] && Y[j] != Q[i]) {
P[i] = Y[j];
}
else if(X[j] == Q[i] && Y[j] != P[i]) {
Q[i] = Y[j];
}
else if(Y[j] == P[i] && X[j] != Q[i]) {
P[i] = X[j];
}
else if(Y[j] == Q[i] && X[j] != P[i]) {
Q[i] = X[j];
}
}
}
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... |