#include "sorting.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
using namespace std;
using pii = pair<int, int>;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int l=-1,r=M,k,T[N],f[N],R[N];
vector<pii> mv;
while(r-l>1){
int m=(l+r)>>1;
k=N;
memset(f,0,sizeof f);
for(int i=0; i<N; i++) T[i]=S[i];
for(int i=0; i<m; i++){
swap(T[X[i]],T[Y[i]]);
}
for(int i=0; i<N; i++){
if(f[i]) continue;
k--;
int x=i;
while(!f[x]){
f[x]=1;
x=T[x];
}
}
if(k<=m) r=m;
else l=m;
}
memset(f,0,sizeof f);
for(int i=0; i<N; i++){
T[i]=S[i];
R[S[i]]=i;
}
for(int i=0; i<r; i++){
swap(T[X[i]],T[Y[i]]);
}
for(int i=0; i<N; i++){
if(f[i]) continue;
int x=i;
while(!f[x]){
f[x]=1;
x=T[x];
if(x!=i) mv.pb({i,x});
}
}
int p=0;
for(int i=0; i<r; i++){
swap(R[S[X[i]]],R[S[Y[i]]]);
swap(S[X[i]],S[Y[i]]);
if(p<(int)mv.size()){
P[p]=R[mv[p].F];
Q[p]=R[mv[p].S];
p++;
}else{
P[p]=Q[p]=0;
p++;
}
}
return r;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
376 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 |
304 KB |
Output is correct |
6 |
Correct |
0 ms |
300 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
376 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 |
304 KB |
Output is correct |
6 |
Correct |
0 ms |
300 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 |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
376 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 |
304 KB |
Output is correct |
6 |
Correct |
0 ms |
300 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 |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
300 KB |
Output is correct |
21 |
Correct |
1 ms |
444 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
428 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
372 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
436 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
2 ms |
440 KB |
Output is correct |
11 |
Correct |
2 ms |
444 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
372 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
376 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
436 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
2 ms |
440 KB |
Output is correct |
11 |
Correct |
2 ms |
444 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
135 ms |
18600 KB |
Output is correct |
16 |
Correct |
150 ms |
19112 KB |
Output is correct |
17 |
Correct |
175 ms |
20060 KB |
Output is correct |
18 |
Correct |
62 ms |
15568 KB |
Output is correct |
19 |
Correct |
120 ms |
18416 KB |
Output is correct |
20 |
Correct |
145 ms |
18996 KB |
Output is correct |
21 |
Correct |
139 ms |
19088 KB |
Output is correct |
22 |
Correct |
125 ms |
15452 KB |
Output is correct |
23 |
Correct |
142 ms |
20820 KB |
Output is correct |
24 |
Correct |
163 ms |
20452 KB |
Output is correct |
25 |
Correct |
150 ms |
20384 KB |
Output is correct |
26 |
Correct |
132 ms |
19152 KB |
Output is correct |
27 |
Correct |
125 ms |
18468 KB |
Output is correct |
28 |
Correct |
154 ms |
20400 KB |
Output is correct |
29 |
Correct |
146 ms |
19884 KB |
Output is correct |
30 |
Correct |
90 ms |
17876 KB |
Output is correct |
31 |
Correct |
142 ms |
20372 KB |
Output is correct |
32 |
Correct |
150 ms |
19996 KB |
Output is correct |
33 |
Correct |
152 ms |
20480 KB |
Output is correct |
34 |
Correct |
146 ms |
20416 KB |
Output is correct |
35 |
Correct |
122 ms |
18304 KB |
Output is correct |
36 |
Correct |
46 ms |
16684 KB |
Output is correct |
37 |
Correct |
155 ms |
20628 KB |
Output is correct |
38 |
Correct |
158 ms |
20244 KB |
Output is correct |
39 |
Correct |
147 ms |
19700 KB |
Output is correct |