#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define debug(x) {cerr<<#x<<"="<<x<<"\n";}
#define debugp(p) {cerr<<#p<<"={"<<p.first<<", "<<p.second<<"}\n";}
#define debug2(x, y) {cerr<<#x<<", "<<#y<<" = "<<x<<", "<<y<<"\n";}
#define pb push_back
#define all(x) x.begin(), x.end()
const int MAXN=200010, LOG=18;
int n, m, ans;
int A[MAXN], B[MAXN], C[MAXN], D[MAXN];
int posB[MAXN], posC[MAXN], posD[MAXN];
int X[MAXN], Y[MAXN], P[MAXN], Q[MAXN];
int Solve(int k){
for (int i=0; i<n; i++){
B[i]=A[i];
C[i]=A[i];
D[A[i]]=A[i];
}
for (int i=0; i<k; i++){
swap(B[X[i]], B[Y[i]]);
// swap(C[X[i]], C[Y[i]]);
}
for (int i=0; i<n; i++){
posB[B[i]]=i;
posC[C[i]]=i;
posD[D[i]]=i;
}
int t=0;
for (int i=0; i<n; i++) if (B[i]!=i){
if (t==k) return 0;
swap(C[X[t]], C[Y[t]]);
swap(posC[C[X[t]]], posC[C[Y[t]]]);
// cerr<<"C: ";for (int j=0; j<n; j++) cerr<<C[j]<<" \n"[j==n-1];
// cerr<<"posC: ";for (int j=0; j<n; j++) cerr<<posC[j]<<" \n"[j==n-1];
int x=i, y=B[i];
// debug2(x, y)
// debug2(D[x], D[y])
// debug2(posC[D[x]], posC[D[y]])
// cerr<<"\n";
swap(D[x], D[y]);
swap(B[posB[x]], B[posB[y]]);
swap(posB[x], posB[y]);
x=D[x];
y=D[y];
P[t]=posC[x];
Q[t]=posC[y];
t++;
}
while (t<k) P[t]=Q[t]=0, t++;
return 1;
}
int findSwapPairs(int _n, int _A[], int _m, int _X[], int _Y[], int _P[], int _Q[]){
n=_n;
for (int i=0; i<n; i++) A[i]=_A[i];
m=_m;
m=min(m, n);
for (int i=0; i<m; i++) X[i]=_X[i], Y[i]=_Y[i];
int dwn=-1, up=n;
while (up-dwn>1){
int mid=(dwn+up)>>1;
if (Solve(mid)) up=mid;
else dwn=mid;
}
ans=up;
Solve(ans);
for (int i=0; i<ans; i++) _P[i]=P[i], _Q[i]=Q[i];
for (int i=0; i<ans; i++){
swap(A[X[i]], A[Y[i]]);
swap(A[P[i]], A[Q[i]]);
}
// for (int i=0; i<n; i++) assert(A[i]==i);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
396 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
396 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
2 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
2 ms |
364 KB |
Output is correct |
21 |
Correct |
2 ms |
492 KB |
Output is correct |
22 |
Correct |
2 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
25 |
Correct |
2 ms |
492 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
3 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
3 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
3 ms |
492 KB |
Output is correct |
10 |
Correct |
3 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
492 KB |
Output is correct |
2 |
Correct |
3 ms |
492 KB |
Output is correct |
3 |
Correct |
3 ms |
492 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
2 ms |
492 KB |
Output is correct |
7 |
Correct |
3 ms |
492 KB |
Output is correct |
8 |
Correct |
2 ms |
492 KB |
Output is correct |
9 |
Correct |
3 ms |
492 KB |
Output is correct |
10 |
Correct |
3 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
2 ms |
492 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
206 ms |
16620 KB |
Output is correct |
16 |
Correct |
229 ms |
19308 KB |
Output is correct |
17 |
Correct |
281 ms |
20076 KB |
Output is correct |
18 |
Correct |
103 ms |
15084 KB |
Output is correct |
19 |
Correct |
195 ms |
17440 KB |
Output is correct |
20 |
Correct |
236 ms |
18188 KB |
Output is correct |
21 |
Correct |
221 ms |
18412 KB |
Output is correct |
22 |
Correct |
203 ms |
21740 KB |
Output is correct |
23 |
Correct |
232 ms |
20588 KB |
Output is correct |
24 |
Correct |
285 ms |
20460 KB |
Output is correct |
25 |
Correct |
265 ms |
20332 KB |
Output is correct |
26 |
Correct |
219 ms |
17772 KB |
Output is correct |
27 |
Correct |
197 ms |
17388 KB |
Output is correct |
28 |
Correct |
254 ms |
20076 KB |
Output is correct |
29 |
Correct |
281 ms |
19556 KB |
Output is correct |
30 |
Correct |
167 ms |
16828 KB |
Output is correct |
31 |
Correct |
298 ms |
19820 KB |
Output is correct |
32 |
Correct |
245 ms |
20204 KB |
Output is correct |
33 |
Correct |
298 ms |
20608 KB |
Output is correct |
34 |
Correct |
263 ms |
20360 KB |
Output is correct |
35 |
Correct |
201 ms |
17516 KB |
Output is correct |
36 |
Correct |
82 ms |
15852 KB |
Output is correct |
37 |
Correct |
304 ms |
20716 KB |
Output is correct |
38 |
Correct |
270 ms |
20208 KB |
Output is correct |
39 |
Correct |
298 ms |
20260 KB |
Output is correct |