#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
vector<int> inv(vector<int> p) {
vector<int> inv(p.size());
for (int i = 0; i < p.size(); ++i) {
inv[p[i]] = i;
}
return inv;
}
int findSwapPairs(int n, int p_[], int M, int X[], int Y[], int P[], int Q[]) {
auto solve = [&](int m) {
auto calc = [&](vector<int> p) {
for (int i = 0; i < m; ++i) {
swap(p[X[i]], p[Y[i]]);
}
return p;
};
vector<int> p(p_, p_ + n);
vector<int> e = calc(p);
vector<int> rev = inv(p), rev_e = inv(e);
auto doSwap = [&](int i, int j, bool wtf = true) {
int x = p[i], y = p[j];
swap(p[i], p[j]);
rev[y] = i, rev[x] = j;
if (wtf) {
int px = rev_e[x], py = rev_e[y];
swap(e[px], e[py]);
rev_e[e[px]] = px, rev_e[e[py]] = py;
}
};
int i = 0;
for (int r = 0; r < m; ++r){
doSwap(X[r], Y[r], 0);
P[r] = 0, Q[r] = 0;
for (; i < n; ++i) {
if (e[i] != i) {
P[r] = rev[i];
Q[r] = rev[e[i]];
break;
}
}
doSwap(P[r], Q[r]);
}
return is_sorted(p.begin(), p.end());
};
int low = -1, high = M;
while (low + 1 < high) {
int m = (low + high) >> 1;
if (solve(m)) {
high = m;
} else {
low = m;
}
}
solve(high);
return high;
}
Compilation message
sorting.cpp: In function 'std::vector<int> inv(std::vector<int>)':
sorting.cpp:8:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
8 | for (int i = 0; i < p.size(); ++i) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
216 KB |
Output is correct |
6 |
Correct |
0 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
216 KB |
Output is correct |
6 |
Correct |
0 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
216 KB |
Output is correct |
6 |
Correct |
0 ms |
300 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 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 |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
2 ms |
572 KB |
Output is correct |
22 |
Correct |
2 ms |
564 KB |
Output is correct |
23 |
Correct |
2 ms |
552 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
476 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
372 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
476 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
372 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
440 KB |
Output is correct |
15 |
Correct |
319 ms |
14580 KB |
Output is correct |
16 |
Correct |
344 ms |
13472 KB |
Output is correct |
17 |
Correct |
433 ms |
14180 KB |
Output is correct |
18 |
Correct |
131 ms |
13812 KB |
Output is correct |
19 |
Correct |
271 ms |
14364 KB |
Output is correct |
20 |
Correct |
296 ms |
14616 KB |
Output is correct |
21 |
Correct |
300 ms |
14596 KB |
Output is correct |
22 |
Correct |
319 ms |
13744 KB |
Output is correct |
23 |
Correct |
341 ms |
14428 KB |
Output is correct |
24 |
Correct |
415 ms |
14540 KB |
Output is correct |
25 |
Correct |
420 ms |
14468 KB |
Output is correct |
26 |
Correct |
286 ms |
15080 KB |
Output is correct |
27 |
Correct |
256 ms |
14604 KB |
Output is correct |
28 |
Correct |
403 ms |
14628 KB |
Output is correct |
29 |
Correct |
384 ms |
14576 KB |
Output is correct |
30 |
Correct |
196 ms |
14792 KB |
Output is correct |
31 |
Correct |
381 ms |
14692 KB |
Output is correct |
32 |
Correct |
333 ms |
14316 KB |
Output is correct |
33 |
Correct |
423 ms |
14096 KB |
Output is correct |
34 |
Correct |
371 ms |
14204 KB |
Output is correct |
35 |
Correct |
281 ms |
14208 KB |
Output is correct |
36 |
Correct |
112 ms |
14752 KB |
Output is correct |
37 |
Correct |
438 ms |
14808 KB |
Output is correct |
38 |
Correct |
410 ms |
14132 KB |
Output is correct |
39 |
Correct |
414 ms |
14116 KB |
Output is correct |