#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int vis[200001],pos[200001];
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]){
bool ch=1;
for (int i=0;i<N;i++)
ch&=(S[i]==i);
if (ch)
return 0;
int l=0,r=M-1,kq=-1;
while (l<=r){
int mid=(l+r)>>1;
for (int i=0;i<=mid;i++)
swap(S[X[i]],S[Y[i]]);
for (int i=0;i<N;i++)
vis[i]=0;
int cnt=N;
for (int i=0;i<N;i++)
if (!vis[i]){
cnt--;
int j=i;
while (!vis[j]){
vis[j]=1;
j=S[j];
}
}
for (int i=mid;i>=0;i--)
swap(S[X[i]],S[Y[i]]);
if (cnt-1<=mid){
kq=mid;
r=mid-1;
}
else
l=mid+1;
}
for (int i=0;i<=kq;i++)
swap(S[X[i]],S[Y[i]]);
int id=0;
for (int i=0;i<N;i++){
while (i!=S[i]){
P[id]=i;
Q[id]=S[i];
swap(S[i],S[S[i]]);
id++;
}
pos[i]=i;
}
for (int i=id;i<=kq;i++)
P[i]=Q[i]=0;
for (int i=kq;i>=0;i--){
P[i]=S[P[i]];
Q[i]=S[Q[i]];
swap(S[pos[X[i]]],S[pos[Y[i]]]);
swap(pos[X[i]],pos[Y[i]]);
}
return kq+1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
600 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
580 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
580 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
108 ms |
17108 KB |
Output is correct |
16 |
Correct |
117 ms |
17744 KB |
Output is correct |
17 |
Correct |
128 ms |
19032 KB |
Output is correct |
18 |
Correct |
36 ms |
13500 KB |
Output is correct |
19 |
Correct |
106 ms |
17744 KB |
Output is correct |
20 |
Correct |
111 ms |
20304 KB |
Output is correct |
21 |
Correct |
113 ms |
18256 KB |
Output is correct |
22 |
Correct |
102 ms |
13908 KB |
Output is correct |
23 |
Correct |
126 ms |
20492 KB |
Output is correct |
24 |
Correct |
130 ms |
19192 KB |
Output is correct |
25 |
Correct |
130 ms |
19364 KB |
Output is correct |
26 |
Correct |
106 ms |
19780 KB |
Output is correct |
27 |
Correct |
96 ms |
19436 KB |
Output is correct |
28 |
Correct |
125 ms |
18708 KB |
Output is correct |
29 |
Correct |
123 ms |
18836 KB |
Output is correct |
30 |
Correct |
75 ms |
16976 KB |
Output is correct |
31 |
Correct |
125 ms |
19508 KB |
Output is correct |
32 |
Correct |
117 ms |
18580 KB |
Output is correct |
33 |
Correct |
131 ms |
19644 KB |
Output is correct |
34 |
Correct |
124 ms |
20564 KB |
Output is correct |
35 |
Correct |
106 ms |
17708 KB |
Output is correct |
36 |
Correct |
41 ms |
15956 KB |
Output is correct |
37 |
Correct |
134 ms |
20396 KB |
Output is correct |
38 |
Correct |
130 ms |
20700 KB |
Output is correct |
39 |
Correct |
133 ms |
19792 KB |
Output is correct |