#include <bits/stdc++.h>
#include "sorting.h"
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 2e5 + 2;
const int mod = 1e9 + 7;
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
int zero = 1;
for (int i = 0; i < n; i++) zero &= s[i] == i;
if (zero == 1) return 0;
vector<int> inv(n);
for (int i = 0; i < n; i++) inv[s[i]] = i;
auto Can = [&] (int mid) {
for (int i = 0; i < m; i++) p[i] = q[i] = 0;
vector<int> a(n);
iota(a.begin(), a.end(), 0);
for (int i = mid; i >= 0; i--) {
swap(a[x[i]], a[y[i]]);
}
vector<int> b(n);
for (int i = 0; i < n; i++) b[a[i]] = i;
int j = mid;
for (int i = 0; i < n; i++) {
if (b[i] == inv[i]) continue;
if (j < 0) return 0;
p[j] = b[i]; q[j--] = inv[i];
swap(a[b[i]], a[inv[i]]);
swap(b[i], b[a[b[i]]]);
}
vector<int> f(n);
iota(f.begin(), f.end(), 0);
vector<int> invf = f;
for (int i = 0; i <= mid; i++) {
swap(f[x[i]], f[y[i]]);
swap(invf[f[x[i]]], invf[f[y[i]]]);
p[i] = invf[p[i]];
q[i] = invf[q[i]];
}
return 1;
};
int l = 0, r = m - 1, ans = m - 1;
while (l <= r) {
int mid = l + r >> 1;
if (Can(mid)) {
ans = mid;
r = mid - 1;
}
else l = mid + 1;
}
Can(ans);
return ans + 1;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:48:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
48 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
296 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
296 KB |
Output is correct |
2 |
Correct |
0 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
308 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
296 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
0 ms |
296 KB |
Output is correct |
14 |
Correct |
0 ms |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
308 KB |
Output is correct |
17 |
Correct |
1 ms |
308 KB |
Output is correct |
18 |
Correct |
0 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 |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
572 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
552 KB |
Output is correct |
25 |
Correct |
2 ms |
564 KB |
Output is correct |
26 |
Correct |
2 ms |
596 KB |
Output is correct |
27 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
480 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
440 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 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 |
488 KB |
Output is correct |
10 |
Correct |
2 ms |
440 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
480 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
440 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 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 |
488 KB |
Output is correct |
10 |
Correct |
2 ms |
440 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
209 ms |
20400 KB |
Output is correct |
16 |
Correct |
233 ms |
20860 KB |
Output is correct |
17 |
Correct |
266 ms |
22176 KB |
Output is correct |
18 |
Correct |
39 ms |
13320 KB |
Output is correct |
19 |
Correct |
188 ms |
22676 KB |
Output is correct |
20 |
Correct |
195 ms |
23532 KB |
Output is correct |
21 |
Correct |
196 ms |
23184 KB |
Output is correct |
22 |
Correct |
206 ms |
17392 KB |
Output is correct |
23 |
Correct |
239 ms |
22984 KB |
Output is correct |
24 |
Correct |
260 ms |
22536 KB |
Output is correct |
25 |
Correct |
269 ms |
22376 KB |
Output is correct |
26 |
Correct |
192 ms |
23256 KB |
Output is correct |
27 |
Correct |
181 ms |
22736 KB |
Output is correct |
28 |
Correct |
250 ms |
22560 KB |
Output is correct |
29 |
Correct |
254 ms |
22392 KB |
Output is correct |
30 |
Correct |
150 ms |
22952 KB |
Output is correct |
31 |
Correct |
267 ms |
23004 KB |
Output is correct |
32 |
Correct |
237 ms |
22008 KB |
Output is correct |
33 |
Correct |
279 ms |
22740 KB |
Output is correct |
34 |
Correct |
251 ms |
22432 KB |
Output is correct |
35 |
Correct |
192 ms |
22200 KB |
Output is correct |
36 |
Correct |
92 ms |
22896 KB |
Output is correct |
37 |
Correct |
280 ms |
23372 KB |
Output is correct |
38 |
Correct |
256 ms |
22216 KB |
Output is correct |
39 |
Correct |
274 ms |
22388 KB |
Output is correct |