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>
#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(), s.end()
using namespace std;
const int N = 2005;
int sz, cnt, res, u[N], a[N], pos[N], asd[N];
void dfs (int v) {
cnt++;
u[v] = 1;
if (u[ a[v] ] == 0)
dfs(a[v]);
}
int findSwapPairs(int n, int b[], int m, int x[], int y[], int p[], int q[]) {
bool fl = 1;
for (int i = 0; i < n; i++) {
a[i] = b[i];
if (a[i] != i)
fl = 0;
}
if (fl)
return 0;
for (int i = 0; i < m; i++) {
swap( a[ x[i] ], a[ y[i] ] );
res = 0;
memset(u, 0, sizeof(u));
for (int j = 0; j < n; j++) {
if (u[j]) continue;
cnt = 0; dfs(j);
res += cnt - 1;
}
if (res <= i + 1) {
for (int j = 0; j < n; j++)
pos[b[j]] = j, asd[a[j]] = j;
int k = 0;
for (int j = 0; j < n; j++) {
if (a[j] == j) continue;
pos[ b[x[k]] ] = y[k];
pos[ b[y[k]] ] = x[k];
swap( b[x[k]], b[y[k]] );
p[k] = pos[a[j]], q[k] = pos[j];
swap( b[pos[ a[j] ]], b[pos[j]] );
swap( pos[ a[j] ], pos[j] );
swap(a[j], a[asd[j]]);
asd[a[j]] = asd[j];
k++;
}
while (k < i) {
p[k] = 0, q[k] = 0;
k++;
}
return i + 1;
}
}
}
Compilation message (stderr)
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:67:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# | 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... |