#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
vector < int > _Q(N), _P(N), _S(N), id(N);
int l = 0, r = M - 1, ans = -1;
while(r >= l) {
auto SWAP = [&](int i, int j) {
swap(id[_S[i]], id[_S[j]]);
swap(_S[i], _S[j]);
};
for(int i = 0; i < N; ++i) _S[i] = S[i];
int m = l + r >> 1;
int swaps = -1;
for(int i = 0; i < m; ++i) {
swap(_S[X[i]], _S[Y[i]]);
}
for(int i = 0; i < N; ++i) {
id[_S[i]] = i;
}
for(int i = 0; i < N; ++i) {
if(id[i] == i) continue;
_P[++swaps] = i;
_Q[swaps] = _S[i];
SWAP(id[i], i);
}
if(swaps + 1 <= m) {
for(int i = 0; i < N; ++i) _S[i] = S[i];
for(int i = 0; i < N; ++i) id[_S[i]] = i;
for(int i = 0; i < m; ++i) {
SWAP(X[i], Y[i]);
if(i <= swaps) {
P[i] = id[_P[i]];
Q[i] = id[_Q[i]];
SWAP(id[_P[i]], id[_Q[i]]);
} else {
P[i] = Q[i] = 0;
}
}
r = m - 1;
ans = m;
} else {
l = m + 1;
}
}
return ans;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:13:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | int m = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
356 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
256 KB |
Output is correct |
14 |
Correct |
0 ms |
256 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
256 KB |
Output is correct |
19 |
Correct |
1 ms |
256 KB |
Output is correct |
20 |
Correct |
1 ms |
256 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
640 KB |
Output is correct |
23 |
Correct |
2 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
640 KB |
Output is correct |
26 |
Correct |
2 ms |
640 KB |
Output is correct |
27 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
544 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
544 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
239 ms |
17656 KB |
Output is correct |
16 |
Correct |
292 ms |
18040 KB |
Output is correct |
17 |
Correct |
296 ms |
19064 KB |
Output is correct |
18 |
Correct |
117 ms |
18424 KB |
Output is correct |
19 |
Correct |
194 ms |
19576 KB |
Output is correct |
20 |
Correct |
225 ms |
20088 KB |
Output is correct |
21 |
Correct |
208 ms |
20088 KB |
Output is correct |
22 |
Correct |
233 ms |
14328 KB |
Output is correct |
23 |
Correct |
292 ms |
19832 KB |
Output is correct |
24 |
Correct |
338 ms |
19448 KB |
Output is correct |
25 |
Correct |
340 ms |
19192 KB |
Output is correct |
26 |
Correct |
202 ms |
20088 KB |
Output is correct |
27 |
Correct |
192 ms |
19704 KB |
Output is correct |
28 |
Correct |
272 ms |
19576 KB |
Output is correct |
29 |
Correct |
280 ms |
19320 KB |
Output is correct |
30 |
Correct |
157 ms |
19832 KB |
Output is correct |
31 |
Correct |
307 ms |
19832 KB |
Output is correct |
32 |
Correct |
286 ms |
19064 KB |
Output is correct |
33 |
Correct |
322 ms |
19576 KB |
Output is correct |
34 |
Correct |
279 ms |
19452 KB |
Output is correct |
35 |
Correct |
207 ms |
19192 KB |
Output is correct |
36 |
Correct |
85 ms |
19832 KB |
Output is correct |
37 |
Correct |
305 ms |
20088 KB |
Output is correct |
38 |
Correct |
282 ms |
19320 KB |
Output is correct |
39 |
Correct |
307 ms |
19448 KB |
Output is correct |