#include <bits/stdc++.h>
using namespace std;
const int c=200002;
int lo, hi, mid, st=0, a[c], ia[c], b[c], ib[c], cs1, cs2;
vector<int> e, f;
void acsere() {swap(ia[a[cs1]], ia[a[cs2]]), swap(a[cs1], a[cs2]);}
void bcsere() {swap(ib[b[cs1]], ib[b[cs2]]), swap(b[cs1], b[cs2]);}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
lo=-1, hi=n;
while(hi-lo>1) {
mid=(hi+lo)/2, st=0;
for (int i=0; i<n; i++) a[i]=s[i], b[i]=i, ia[s[i]]=i, ib[i]=i;
e.clear(), f.clear();
for (int i=mid-1; i>=0; i--) swap(ib[b[x[i]]], ib[b[y[i]]]), swap(b[x[i]], b[y[i]]);
cs1=x[0], cs2=y[0];
if (mid) acsere(), bcsere();
for (int i=0; i<n; i++) {
if (ia[i]!=ib[i]) {
cs1=ia[i], cs2=ib[i];
e.push_back(cs1), f.push_back(cs2);
acsere();
st++;
if (st<mid) {
cs1=x[st], cs2=y[st];
acsere(), bcsere();
}
}
}
while(st<mid) e.push_back(0), f.push_back(0), st++;
if (st<=mid) {
hi=mid;
for (int i=0; i<mid; i++) p[i]=e[i], q[i]=f[i];
}
else lo=mid;
}
return hi;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:9:39: warning: unused parameter 'm' [-Wunused-parameter]
9 | int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
| ~~~~^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 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 |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 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 |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
0 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
640 KB |
Output is correct |
24 |
Correct |
1 ms |
640 KB |
Output is correct |
25 |
Correct |
1 ms |
640 KB |
Output is correct |
26 |
Correct |
1 ms |
640 KB |
Output is correct |
27 |
Correct |
1 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
560 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 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 |
560 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 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 |
224 ms |
14292 KB |
Output is correct |
16 |
Correct |
283 ms |
14564 KB |
Output is correct |
17 |
Correct |
368 ms |
15204 KB |
Output is correct |
18 |
Correct |
75 ms |
10860 KB |
Output is correct |
19 |
Correct |
233 ms |
12652 KB |
Output is correct |
20 |
Correct |
260 ms |
13796 KB |
Output is correct |
21 |
Correct |
255 ms |
13796 KB |
Output is correct |
22 |
Correct |
216 ms |
15408 KB |
Output is correct |
23 |
Correct |
233 ms |
15716 KB |
Output is correct |
24 |
Correct |
390 ms |
15588 KB |
Output is correct |
25 |
Correct |
371 ms |
15332 KB |
Output is correct |
26 |
Correct |
241 ms |
13412 KB |
Output is correct |
27 |
Correct |
223 ms |
12752 KB |
Output is correct |
28 |
Correct |
367 ms |
14948 KB |
Output is correct |
29 |
Correct |
342 ms |
14056 KB |
Output is correct |
30 |
Correct |
141 ms |
11244 KB |
Output is correct |
31 |
Correct |
347 ms |
14232 KB |
Output is correct |
32 |
Correct |
245 ms |
14312 KB |
Output is correct |
33 |
Correct |
385 ms |
14592 KB |
Output is correct |
34 |
Correct |
300 ms |
14564 KB |
Output is correct |
35 |
Correct |
225 ms |
11628 KB |
Output is correct |
36 |
Correct |
60 ms |
10604 KB |
Output is correct |
37 |
Correct |
375 ms |
15076 KB |
Output is correct |
38 |
Correct |
372 ms |
14368 KB |
Output is correct |
39 |
Correct |
375 ms |
14436 KB |
Output is correct |