#include "sorting.h"
#include <cassert>
#include <iostream>
#include <algorithm>
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int A[N], pos[N];
for (int i = 0; i < N; ++i)
A[i] = i;
for (int i = M - 1; i >= 1; --i)
swap(A[X[i]], A[Y[i]]);/*
for (int i = 0; i < M; ++i)
cerr << P[i] << " " << Q[i] << '\n';
cerr << "---------\n";*/
swap(S[X[0]], S[Y[0]]);
int r = 0;
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j)
pos[A[j]] = j;
for (int j = 0; j < N; ++j) {
if (pos[S[j]] != j) {
Q[i] = j, P[i] = pos[S[j]];
++r;
break;
}
}
if (Q[i] == P[i])
break;
swap(S[Q[i]], S[P[i]]);
assert(S[Q[i]] == A[Q[i]]);
if (i + 1 < M) {
swap(S[X[i + 1]], S[Y[i + 1]]);
swap(A[X[i + 1]], A[Y[i + 1]]);
}
}/*
for (int i = 0; i < M; ++i) {
cerr << X[i] << " " << Y[i] << '\n';
if (i < r)
cerr << P[i] << " " << Q[i] << '\n';
}
for (int i = 0; i < N; ++i)
cerr << S[i] << " \n"[i + 1 == N];*/
return r;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Runtime error |
1 ms |
332 KB |
Execution killed with signal 6 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
460 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |