#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int T[N];
for (int i = 0; i < N; i++) T[i] = S[i];
for (int i = 0; i < M; i++) swap(T[X[i]], T[Y[i]]);
for (int i = 0; i < M; i++) P[i] = Q[i] = 0;
vector<pair<int, int>> swap_list;
for (int i = 0; i < N; i++) {
if (T[i] != i) {
swap_list.emplace_back(i, T[i]);
for (int j = i+1; j < N; j++) {
if (T[j] == i) {
swap(T[i], T[j]);
}
}
}
}
for (int i = 0; i < N; i++) T[i] = S[i];
for (int i = 0; i < (int)swap_list.size(); i++) {
swap(T[X[i]], T[Y[i]]);
int a, b;
for (int j = 0; j < N; j++) {
if (T[j] == swap_list[i].first) a = j;
if (T[j] == swap_list[i].second) b = j;
}
P[i] = a;
Q[i] = b;
swap(T[a], T[b]);
}
for (int i = 0; i < M; i++) {
if (is_sorted(S, S+N)) return i;
swap(S[X[i]], S[Y[i]]);
swap(S[P[i]], S[Q[i]]);
}
return M;
}
// int main(int argc, char *argv[]) {
// if (argc > 1) srand(atoi(argv[1]));
// int N = rand() % 50 + 1;
// int M = 30*N;
// int S[N], X[M], Y[M], P[M], Q[M];
// iota(S, S+N, 0);
// random_shuffle(S, S+N);
// for (int i = 0; i < N; i++) {
// cerr << S[i] << " \n"[i == N-1];
// }
// for (int i = 0; i < M; i++) {
// int a = rand() % N, b = rand() % N;
// if (a > b) swap(a, b);
// X[i] = 0, Y[i] = 0;
// }
// findSwapPairs(N, S, M, X, Y, P, Q);
// return 0;
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
280 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
304 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
280 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
224 KB |
Output is correct |
19 |
Correct |
1 ms |
224 KB |
Output is correct |
20 |
Correct |
1 ms |
224 KB |
Output is correct |
21 |
Correct |
3 ms |
692 KB |
Output is correct |
22 |
Correct |
3 ms |
608 KB |
Output is correct |
23 |
Correct |
4 ms |
696 KB |
Output is correct |
24 |
Correct |
4 ms |
712 KB |
Output is correct |
25 |
Correct |
3 ms |
608 KB |
Output is correct |
26 |
Correct |
3 ms |
732 KB |
Output is correct |
27 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
436 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |