#include <iostream>
#include <vector>
#include "sorting.h"
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int l = 0;
int r = M;
int ans = -1;
auto check = [&](int m) {
vector<int> nums(S, S + N);
for (int i = 0; i < m; i++) {
swap(nums[X[i]], nums[Y[i]]);
}
vector<bool> vis(N);
int cycle_count = 0;
for (int i = 0; i < N; i++) {
if (!vis[i]) {
cycle_count++;
int cur = nums[i];
while (cur != i) {
vis[cur] = true;
cur = nums[cur];
}
}
}
return N - cycle_count <= m;
};
while (l <= r) {
int m = (l + r) / 2;
if (check(m)) {
ans = m;
r = m - 1;
} else {
l = m + 1;
}
}
vector<int> after_m(S, S + N);
for (int i = 0; i < ans; i++) {
swap(after_m[X[i]], after_m[Y[i]]);
}
vector<pair<int, int>> nums_to_swap;
vector<bool> vis(N);
for (int i = 0; i < N; i++) {
if (!vis[i]) {
int nxt = after_m[i];
int pre = i;
vis[i] = true;
while (!vis[nxt]) {
vis[nxt] = true;
nums_to_swap.push_back({pre, nxt});
pre = nxt;
nxt = after_m[nxt];
}
}
}
while ((int) nums_to_swap.size() < ans) {
nums_to_swap.push_back({0, 0});
}
vector<int> cur_p(S, S + N);
vector<int> cur_inv(N);
for (int i = 0; i < N; i++) {
cur_inv[cur_p[i]] = i;
}
for (int i = 0; i < ans; i++) {
// their swap
auto t1 = cur_p[X[i]];
auto t2 = cur_p[Y[i]];
swap(cur_p[X[i]], cur_p[Y[i]]);
swap(cur_inv[t1], cur_inv[t2]);
// our swap
auto [num1, num2] = nums_to_swap[i];
auto idx1 = cur_inv[num1];
auto idx2 = cur_inv[num2];
P[i] = idx1;
Q[i] = idx2;
swap(cur_p[idx1], cur_p[idx2]);
swap(cur_inv[num1], cur_inv[num2]);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 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 |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
304 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 |
212 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 |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
340 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 |
304 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 |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
300 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
440 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
440 KB |
Output is correct |
26 |
Correct |
2 ms |
436 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
440 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
122 ms |
18528 KB |
Output is correct |
16 |
Correct |
129 ms |
19088 KB |
Output is correct |
17 |
Correct |
138 ms |
19944 KB |
Output is correct |
18 |
Correct |
57 ms |
15540 KB |
Output is correct |
19 |
Correct |
115 ms |
17880 KB |
Output is correct |
20 |
Correct |
130 ms |
18624 KB |
Output is correct |
21 |
Correct |
122 ms |
18520 KB |
Output is correct |
22 |
Correct |
119 ms |
15396 KB |
Output is correct |
23 |
Correct |
132 ms |
20796 KB |
Output is correct |
24 |
Correct |
142 ms |
20340 KB |
Output is correct |
25 |
Correct |
137 ms |
20184 KB |
Output is correct |
26 |
Correct |
124 ms |
18492 KB |
Output is correct |
27 |
Correct |
109 ms |
17904 KB |
Output is correct |
28 |
Correct |
141 ms |
20380 KB |
Output is correct |
29 |
Correct |
136 ms |
19732 KB |
Output is correct |
30 |
Correct |
83 ms |
18108 KB |
Output is correct |
31 |
Correct |
137 ms |
20132 KB |
Output is correct |
32 |
Correct |
130 ms |
19892 KB |
Output is correct |
33 |
Correct |
136 ms |
20536 KB |
Output is correct |
34 |
Correct |
133 ms |
20320 KB |
Output is correct |
35 |
Correct |
115 ms |
17744 KB |
Output is correct |
36 |
Correct |
55 ms |
16744 KB |
Output is correct |
37 |
Correct |
147 ms |
20484 KB |
Output is correct |
38 |
Correct |
140 ms |
20064 KB |
Output is correct |
39 |
Correct |
135 ms |
19572 KB |
Output is correct |