#include "sorting.h"
#ifdef ngu
#include "grader.cpp"
#endif // ngu
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[2][N], pos[2][N];
int cnt;
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
int l = 0, r = m, ans = -1;
auto check = [&] (int rounds) {
for(int i = 0; i < n; i++) a[0][i] = a[1][i] = s[i];
for(int i = 0; i < rounds; i++) {
swap(a[1][x[i]], a[1][y[i]]);
}
cnt = 0;
auto Swap = [&] (int j, int val1, int val2) {
if (j == 0) {
p[cnt] = pos[j][val1];
q[cnt] = pos[j][val2];
}
swap(pos[j][val1], pos[j][val2]);
swap(a[j][pos[j][val1]], a[j][pos[j][val2]]);
};
for(int i = 0; i < n; i++) pos[0][a[0][i]] = i, pos[1][a[1][i]] = i;
for(int i = 0; i < n; i++) if (a[1][i] != i) {
if (cnt >= rounds) {
return false;
}
Swap(0, a[0][x[cnt]], a[0][y[cnt]]);
Swap(0, a[1][i], i);
cnt++;
Swap(1, a[1][i], i);
}
for(int i = cnt; i < rounds; i++) p[i] = q[i] = 0;
return true;
};
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) {
ans = mid;
r = mid - 1;
} else l = mid + 1;
}
assert(ans != -1);
check(ans);
return ans;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:53:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2648 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2648 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
2 ms |
2420 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
2 ms |
2420 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
15 |
Correct |
126 ms |
12460 KB |
Output is correct |
16 |
Correct |
150 ms |
20564 KB |
Output is correct |
17 |
Correct |
181 ms |
22868 KB |
Output is correct |
18 |
Correct |
60 ms |
19792 KB |
Output is correct |
19 |
Correct |
127 ms |
21764 KB |
Output is correct |
20 |
Correct |
141 ms |
21500 KB |
Output is correct |
21 |
Correct |
135 ms |
21504 KB |
Output is correct |
22 |
Correct |
132 ms |
17112 KB |
Output is correct |
23 |
Correct |
151 ms |
22520 KB |
Output is correct |
24 |
Correct |
203 ms |
23032 KB |
Output is correct |
25 |
Correct |
182 ms |
21952 KB |
Output is correct |
26 |
Correct |
133 ms |
22508 KB |
Output is correct |
27 |
Correct |
114 ms |
20976 KB |
Output is correct |
28 |
Correct |
194 ms |
22096 KB |
Output is correct |
29 |
Correct |
175 ms |
21696 KB |
Output is correct |
30 |
Correct |
96 ms |
20396 KB |
Output is correct |
31 |
Correct |
196 ms |
22024 KB |
Output is correct |
32 |
Correct |
159 ms |
21588 KB |
Output is correct |
33 |
Correct |
195 ms |
22224 KB |
Output is correct |
34 |
Correct |
190 ms |
22172 KB |
Output is correct |
35 |
Correct |
139 ms |
21572 KB |
Output is correct |
36 |
Correct |
45 ms |
19900 KB |
Output is correct |
37 |
Correct |
187 ms |
23604 KB |
Output is correct |
38 |
Correct |
187 ms |
21760 KB |
Output is correct |
39 |
Correct |
184 ms |
21996 KB |
Output is correct |