#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
auto check = [&](int d){
vector<int> p(N);
for(int i=0;i<N;i++) p[i]=S[i];
for(int i=0;i<d;i++){
swap(p[X[i]],p[Y[i]]);
P[i]=Q[i]=0;
}
int k=0;
vector<bool> vis(N);
for(int i=0;i<N;i++){
if(!vis[i]){
int u=i;
while(!vis[u]){
vis[u]=true;
if(p[u]!=i) P[k]=u,Q[k++]=p[u];
u=p[u];
}
}
}
if(k<=d){
vector<int> f(N);
for(int i=0;i<N;i++) f[S[i]]=i,p[i]=S[i];
for(int i=0;i<k;i++){
swap(p[X[i]],p[Y[i]]);
swap(f[p[X[i]]],f[p[Y[i]]]);
int x=f[P[i]],y=f[Q[i]];
swap(p[x],p[y]);
swap(f[p[x]],f[p[y]]);
P[i]=x,Q[i]=y;
}
}
return k<=d;
};
int l=0,r=M,res=M;
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)) res=mid,r=mid-1;
else l=mid+1;
}
check(res);
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
444 KB |
Output is correct |
4 |
Correct |
1 ms |
444 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
436 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
436 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
424 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
444 KB |
Output is correct |
16 |
Correct |
1 ms |
444 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
1 ms |
436 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
700 KB |
Output is correct |
23 |
Correct |
1 ms |
704 KB |
Output is correct |
24 |
Correct |
1 ms |
572 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
708 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
544 KB |
Output is correct |
3 |
Correct |
1 ms |
444 KB |
Output is correct |
4 |
Correct |
1 ms |
444 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
508 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
1 ms |
508 KB |
Output is correct |
10 |
Correct |
1 ms |
580 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
444 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
544 KB |
Output is correct |
3 |
Correct |
1 ms |
444 KB |
Output is correct |
4 |
Correct |
1 ms |
444 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
508 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
448 KB |
Output is correct |
9 |
Correct |
1 ms |
508 KB |
Output is correct |
10 |
Correct |
1 ms |
580 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
444 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
156 ms |
17240 KB |
Output is correct |
16 |
Correct |
160 ms |
17892 KB |
Output is correct |
17 |
Correct |
160 ms |
19696 KB |
Output is correct |
18 |
Correct |
79 ms |
17220 KB |
Output is correct |
19 |
Correct |
132 ms |
18240 KB |
Output is correct |
20 |
Correct |
143 ms |
18684 KB |
Output is correct |
21 |
Correct |
142 ms |
19756 KB |
Output is correct |
22 |
Correct |
144 ms |
13980 KB |
Output is correct |
23 |
Correct |
186 ms |
20400 KB |
Output is correct |
24 |
Correct |
165 ms |
20168 KB |
Output is correct |
25 |
Correct |
178 ms |
18992 KB |
Output is correct |
26 |
Correct |
137 ms |
18652 KB |
Output is correct |
27 |
Correct |
120 ms |
18284 KB |
Output is correct |
28 |
Correct |
158 ms |
19196 KB |
Output is correct |
29 |
Correct |
161 ms |
18700 KB |
Output is correct |
30 |
Correct |
91 ms |
18420 KB |
Output is correct |
31 |
Correct |
166 ms |
20124 KB |
Output is correct |
32 |
Correct |
152 ms |
19560 KB |
Output is correct |
33 |
Correct |
170 ms |
19176 KB |
Output is correct |
34 |
Correct |
164 ms |
18972 KB |
Output is correct |
35 |
Correct |
124 ms |
18960 KB |
Output is correct |
36 |
Correct |
60 ms |
19428 KB |
Output is correct |
37 |
Correct |
160 ms |
20016 KB |
Output is correct |
38 |
Correct |
150 ms |
18784 KB |
Output is correct |
39 |
Correct |
168 ms |
19232 KB |
Output is correct |