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;
const int MAXN = 2005;
int n, m;
int s[MAXN], pos[MAXN];
int cnt[MAXN];
void do_swap(int x, int y) {
int p = s[x], q = s[y];
swap(pos[p], pos[q]);
swap(s[x], s[y]);
//for (int i = 0; i < n; ++i) cerr << s[i] << ' '; cerr << endl;
}
bool is_sorted() {
for (int i = 0; i < n; ++i) if (s[i] != i) return false;
return true;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N;
m = M;
for (int i = 0; i < n; ++i) {
s[i] = S[i];
pos[s[i]] = i;
}
if (is_sorted()) return 0;
for (int i = 0; i < m; ++i) {
++cnt[X[i]];
++cnt[Y[i]];
}
for (int i = 0; i < m; ++i) {
if (is_sorted()) return i;
do_swap(X[i], Y[i]);
--cnt[X[i]];
--cnt[Y[i]];
bool found = false;
for (int j = 0; j < n; ++j) {
if (pos[j] != j && cnt[j] == 0) {
found = true;
P[i] = j; Q[i] = pos[j];
do_swap(j, pos[j]);
break;
}
}
if (!found) P[i] = Q[i] = 0, do_swap(0, 0);
}
for (int i = 0; i < n; ++i) assert(s[i] == i);
return m;
}
# | 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... |