#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;
int cp[N];
for(int i = 0;i < N;i++) cp[i] = S[i];
while(l <= r){
R = 0;
for(int i = 0;i < N;i++) S[i] = cp[i];
m = l+r>>1;
for(int i = 0;i <= m;i++){
if(is_sorted(S, S+N)) break;
R++;
swap(S[X[i]], S[Y[i]]);
int opt[N];
for(int j = 0;j<N;j++) opt[j]=j;
for(int j = m;j > i;j--) swap(opt[X[j]], opt[Y[j]]);
bool f = false;
for(int j = 0;j < N;j++){
if(S[j] != opt[j]){
f = true;
for(int k = j+1;k < N;k++) {
if(S[k] == opt[j]) {
p[R-1] = j;
q[R-1] = k;
swap(S[j], S[k]);
break;
}
}
break;
}
}
if(!f) p[R-1] = q[R-1] = 0;
}
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:14:8: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
14 | m = l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
7 |
Correct |
1 ms |
212 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 |
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 |
7 |
Correct |
1 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 |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
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 |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 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 |
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 |
7 |
Correct |
1 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 |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
4 ms |
340 KB |
Output is correct |
22 |
Correct |
4 ms |
340 KB |
Output is correct |
23 |
Correct |
5 ms |
448 KB |
Output is correct |
24 |
Correct |
4 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
456 KB |
Output is correct |
26 |
Correct |
3 ms |
340 KB |
Output is correct |
27 |
Correct |
4 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
404 KB |
Output is correct |
2 |
Correct |
68 ms |
480 KB |
Output is correct |
3 |
Correct |
52 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
20 ms |
468 KB |
Output is correct |
6 |
Correct |
24 ms |
472 KB |
Output is correct |
7 |
Correct |
30 ms |
468 KB |
Output is correct |
8 |
Correct |
54 ms |
468 KB |
Output is correct |
9 |
Correct |
55 ms |
488 KB |
Output is correct |
10 |
Correct |
73 ms |
444 KB |
Output is correct |
11 |
Correct |
65 ms |
488 KB |
Output is correct |
12 |
Correct |
32 ms |
480 KB |
Output is correct |
13 |
Correct |
76 ms |
476 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
404 KB |
Output is correct |
2 |
Correct |
68 ms |
480 KB |
Output is correct |
3 |
Correct |
52 ms |
464 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
20 ms |
468 KB |
Output is correct |
6 |
Correct |
24 ms |
472 KB |
Output is correct |
7 |
Correct |
30 ms |
468 KB |
Output is correct |
8 |
Correct |
54 ms |
468 KB |
Output is correct |
9 |
Correct |
55 ms |
488 KB |
Output is correct |
10 |
Correct |
73 ms |
444 KB |
Output is correct |
11 |
Correct |
65 ms |
488 KB |
Output is correct |
12 |
Correct |
32 ms |
480 KB |
Output is correct |
13 |
Correct |
76 ms |
476 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Execution timed out |
1047 ms |
18288 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |