#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[]) {
int l = 0, r = M, bes = M;
while(l <= r){
vector<int> seq(N);
for(int i = 0; i < N; ++i)seq[i] = S[i];
int m = (l+r)>>1;
for(int i = 0; i < m; ++i)swap(seq[X[i]],seq[Y[i]]);
int cn = 0;
for(int i = 0; i < N; ++i){
while(seq[i] != i){
cn++;
swap(seq[i],seq[seq[i]]);
}
}
if(cn > m)l = m+1;
else bes = m, r = m-1;
}
map<int,int> Ma;
for(int i = 0; i < N; ++i)Ma[S[i]] = i;
vector<int> seq(N);
for(int i = 0; i < N; ++i)seq[i] = S[i];
for(int i = 0; i < bes; ++i)swap(seq[X[i]],seq[Y[i]]);
int cn = 0;
for(int i = 0; i < N; ++i){
while(seq[i] != i){
P[cn] = seq[i];
Q[cn] = seq[seq[i]];
cn++;
swap(seq[i],seq[seq[i]]);
}
}
for(int i = 0; i < bes; ++i){
swap(S[X[i]],S[Y[i]]);
swap(Ma[S[X[i]]],Ma[S[Y[i]]]);
P[i] = Ma[P[i]];
Q[i] = Ma[Q[i]];
swap(S[P[i]],S[Q[i]]);
swap(Ma[S[P[i]]],Ma[S[Q[i]]]);
}
return bes;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
304 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
304 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
1 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 |
304 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 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 |
304 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
304 KB |
Output is correct |
8 |
Correct |
1 ms |
292 KB |
Output is correct |
9 |
Correct |
1 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 |
304 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 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 |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 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 |
2 ms |
440 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
436 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
436 KB |
Output is correct |
10 |
Correct |
2 ms |
436 KB |
Output is correct |
11 |
Correct |
3 ms |
440 KB |
Output is correct |
12 |
Correct |
2 ms |
436 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
440 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
3 ms |
436 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
436 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
3 ms |
436 KB |
Output is correct |
10 |
Correct |
2 ms |
436 KB |
Output is correct |
11 |
Correct |
3 ms |
440 KB |
Output is correct |
12 |
Correct |
2 ms |
436 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
366 ms |
24792 KB |
Output is correct |
16 |
Correct |
434 ms |
25212 KB |
Output is correct |
17 |
Correct |
552 ms |
27064 KB |
Output is correct |
18 |
Correct |
92 ms |
22712 KB |
Output is correct |
19 |
Correct |
284 ms |
25948 KB |
Output is correct |
20 |
Correct |
324 ms |
26856 KB |
Output is correct |
21 |
Correct |
276 ms |
26872 KB |
Output is correct |
22 |
Correct |
357 ms |
22080 KB |
Output is correct |
23 |
Correct |
404 ms |
27584 KB |
Output is correct |
24 |
Correct |
503 ms |
27388 KB |
Output is correct |
25 |
Correct |
546 ms |
27496 KB |
Output is correct |
26 |
Correct |
330 ms |
26684 KB |
Output is correct |
27 |
Correct |
304 ms |
25844 KB |
Output is correct |
28 |
Correct |
497 ms |
27136 KB |
Output is correct |
29 |
Correct |
430 ms |
27216 KB |
Output is correct |
30 |
Correct |
226 ms |
25388 KB |
Output is correct |
31 |
Correct |
479 ms |
27772 KB |
Output is correct |
32 |
Correct |
417 ms |
26612 KB |
Output is correct |
33 |
Correct |
517 ms |
27720 KB |
Output is correct |
34 |
Correct |
447 ms |
27416 KB |
Output is correct |
35 |
Correct |
295 ms |
25632 KB |
Output is correct |
36 |
Correct |
81 ms |
24396 KB |
Output is correct |
37 |
Correct |
581 ms |
28860 KB |
Output is correct |
38 |
Correct |
486 ms |
27312 KB |
Output is correct |
39 |
Correct |
457 ms |
27860 KB |
Output is correct |