#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[]) {
if(is_sorted(S, S+N)) return 0;
M = min(M, 4*N);
int p[M], q[M];
int R, l = 0, r = M-1, m, b = M, cur;
int cp[N];
for(int i = 0;i < N;i++) cp[i] = S[i];
int opt[N], ipt[N], is[N];
while(l <= r){
R = 0;
for(int i = 0;i < N;i++) {
S[i] = cp[i];
is[S[i]] = i;
}
m = l+r>>1;
for(int j = 0;j<N;j++) opt[j]=ipt[j]=j;
for(int j = m;j>=0;j--) {
swap(ipt[opt[X[j]]], ipt[opt[Y[j]]]);
swap(opt[X[j]], opt[Y[j]]);
}
cur = 0;
for(int i = 0;i <= m;i++){
R++;
swap(is[S[X[i]]], is[S[Y[i]]]);
swap(S[X[i]], S[Y[i]]);
swap(ipt[opt[X[i]]], ipt[opt[Y[i]]]);
swap(opt[X[i]], opt[Y[i]]);
while(cur < N && is[cur] == ipt[cur])cur++;
if(cur == N){
p[R-1] = q[R-1] = 0;
continue;
}
p[R-1] = is[cur];
q[R-1] = ipt[cur];
int sipt = S[ipt[cur]];
swap(S[is[cur]], S[ipt[cur]]);
swap(is[cur], is[sipt]);
}
if(is_sorted(S, S+N)){
for(int i = 0;i < R;i++) {
P[i] = p[i];
Q[i] = q[i];
}
b = R;
r = m-1;
} else l = m+1;
}
return b;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:18:8: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
18 | m = l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 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 |
0 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 |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 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 |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 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 |
0 ms |
212 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 |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 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 |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
468 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 |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
107 ms |
16460 KB |
Output is correct |
16 |
Correct |
141 ms |
24592 KB |
Output is correct |
17 |
Correct |
178 ms |
25956 KB |
Output is correct |
18 |
Correct |
36 ms |
13384 KB |
Output is correct |
19 |
Correct |
121 ms |
25328 KB |
Output is correct |
20 |
Correct |
136 ms |
26036 KB |
Output is correct |
21 |
Correct |
123 ms |
26044 KB |
Output is correct |
22 |
Correct |
96 ms |
21340 KB |
Output is correct |
23 |
Correct |
141 ms |
26956 KB |
Output is correct |
24 |
Correct |
169 ms |
26540 KB |
Output is correct |
25 |
Correct |
171 ms |
26156 KB |
Output is correct |
26 |
Correct |
122 ms |
25996 KB |
Output is correct |
27 |
Correct |
112 ms |
25388 KB |
Output is correct |
28 |
Correct |
156 ms |
26420 KB |
Output is correct |
29 |
Correct |
147 ms |
26108 KB |
Output is correct |
30 |
Correct |
92 ms |
24972 KB |
Output is correct |
31 |
Correct |
155 ms |
26632 KB |
Output is correct |
32 |
Correct |
116 ms |
26060 KB |
Output is correct |
33 |
Correct |
165 ms |
26624 KB |
Output is correct |
34 |
Correct |
141 ms |
26480 KB |
Output is correct |
35 |
Correct |
112 ms |
24872 KB |
Output is correct |
36 |
Correct |
51 ms |
24352 KB |
Output is correct |
37 |
Correct |
166 ms |
27340 KB |
Output is correct |
38 |
Correct |
159 ms |
26188 KB |
Output is correct |
39 |
Correct |
170 ms |
26344 KB |
Output is correct |