#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int n;
int a[200000], b[200000];
int m;
int x[600000], y[600000];
bool check(int val){
for(int i=0;i<n;i++)
b[i] = a[i];
for(int i=0;i<val;i++)
swap(b[x[i]], b[y[i]]);
int cnt = n;
for(int i=0;i<n;i++){
if (b[i] == -1) continue;
cnt--;
int index = i;
do{
int nex = b[index];
b[index] = -1;
index = nex;
}while(index != i);
}
return cnt <= val;
}
int curPos[200000];
int trace(int val,int P[], int Q[]){
for(int i=0;i<n;i++)
b[i] = a[i],
curPos[a[i]] = i;
for(int i=0;i<val;i++)
swap(b[x[i]], b[y[i]]);
int cnt = 0;
for(int i=0;i<n;i++){
if (b[i] == -1) continue;
int index = i;
for(int _=0;b[index] != -1;_++){
int nex = b[index];
if (nex == i) {b[index] = -1; break;}
//~ cout << index << ' ' << nex << endl;
swap(curPos[a[x[cnt]]], curPos[a[y[cnt]]]);
swap(a[x[cnt]], a[y[cnt]]);
//~ for(int j=0;j<n;j++) cout << a[j] << ' '; cout << endl;
P[cnt] = curPos[b[index]];
Q[cnt] = curPos[b[nex]];
swap(curPos[a[P[cnt]]], curPos[a[Q[cnt]]]);
swap(a[P[cnt]], a[Q[cnt]]);
++cnt;
//~ for(int j=0;j<n;j++) cout << a[j] << ' '; cout << endl;
b[index] = -1;
index = nex;
}
}
while(cnt < val) P[cnt] = Q[cnt] = 0, ++cnt;
return val;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N;
for(int i=0;i<n;i++)
a[i] = S[i];
m = M;
for(int i=0;i<m;i++)
x[i] = X[i],
y[i] = Y[i];
int l=-1, r=m;
while(l+1<r){
int mid = (l+r)/2;
if (check(mid)) r = mid;
else l = mid;
}
return trace(r,P,Q);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
3 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
640 KB |
Output is correct |
22 |
Correct |
3 ms |
640 KB |
Output is correct |
23 |
Correct |
4 ms |
640 KB |
Output is correct |
24 |
Correct |
2 ms |
640 KB |
Output is correct |
25 |
Correct |
3 ms |
640 KB |
Output is correct |
26 |
Correct |
3 ms |
640 KB |
Output is correct |
27 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
3 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
3 ms |
512 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
148 ms |
14472 KB |
Output is correct |
16 |
Correct |
192 ms |
14628 KB |
Output is correct |
17 |
Correct |
266 ms |
15804 KB |
Output is correct |
18 |
Correct |
67 ms |
11896 KB |
Output is correct |
19 |
Correct |
145 ms |
14456 KB |
Output is correct |
20 |
Correct |
162 ms |
14968 KB |
Output is correct |
21 |
Correct |
153 ms |
14984 KB |
Output is correct |
22 |
Correct |
137 ms |
15736 KB |
Output is correct |
23 |
Correct |
190 ms |
15992 KB |
Output is correct |
24 |
Correct |
181 ms |
16028 KB |
Output is correct |
25 |
Correct |
198 ms |
16120 KB |
Output is correct |
26 |
Correct |
166 ms |
14840 KB |
Output is correct |
27 |
Correct |
124 ms |
14328 KB |
Output is correct |
28 |
Correct |
239 ms |
15716 KB |
Output is correct |
29 |
Correct |
186 ms |
15732 KB |
Output is correct |
30 |
Correct |
101 ms |
13688 KB |
Output is correct |
31 |
Correct |
196 ms |
16092 KB |
Output is correct |
32 |
Correct |
230 ms |
15356 KB |
Output is correct |
33 |
Correct |
212 ms |
16096 KB |
Output is correct |
34 |
Correct |
176 ms |
16024 KB |
Output is correct |
35 |
Correct |
131 ms |
14328 KB |
Output is correct |
36 |
Correct |
48 ms |
12792 KB |
Output is correct |
37 |
Correct |
204 ms |
17016 KB |
Output is correct |
38 |
Correct |
196 ms |
15992 KB |
Output is correct |
39 |
Correct |
184 ms |
16604 KB |
Output is correct |