#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=2e5+7;
int F[LIM];
int fnd(int x) {
if(F[x]==x) return x;
return F[x]=fnd(F[x]);
}
bool uni(int a, int b) {
if(fnd(a)==fnd(b)) return false;
F[fnd(b)]=fnd(a);
return true;
}
int findSwapPairs(int n, int S[], int m, int X[], int Y[], int P[], int Q[]) {
int po=0, ko=m;
while(po<ko) {
int sr=(po+ko)/2;
vector<int>T(n);
rep(i, n) T[i]=S[i];
rep(i, sr) swap(T[X[i]], T[Y[i]]);
rep(i, n) F[i]=i;
int p=0;
rep(i, n) if(uni(i, T[i])) ++p;
if(p<=sr) ko=sr; else po=sr+1;
}
m=po;
vector<int>T(n);
rep(i, n) T[i]=S[i];
vector<int>C=T;
rep(i, m) swap(T[X[i]], T[Y[i]]);
vector<int>Z(n);
rep(i, n) Z[C[i]]=i;
int l=0;
rep(i, m) {
swap(C[X[i]], C[Y[i]]);
swap(Z[C[X[i]]], Z[C[Y[i]]]);
while(l<n && T[l]==l) ++l;
if(l==n) {
P[i]=Q[i]=0;
continue;
}
int a=Z[T[l]], b=Z[T[T[l]]];
P[i]=a;
Q[i]=b;
swap(C[a], C[b]);
swap(Z[C[a]], Z[C[b]]);
swap(T[l], T[T[l]]);
}
return m;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
400 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
400 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
94 ms |
9656 KB |
Output is correct |
16 |
Correct |
102 ms |
17540 KB |
Output is correct |
17 |
Correct |
125 ms |
20572 KB |
Output is correct |
18 |
Correct |
56 ms |
16304 KB |
Output is correct |
19 |
Correct |
106 ms |
18136 KB |
Output is correct |
20 |
Correct |
127 ms |
18508 KB |
Output is correct |
21 |
Correct |
107 ms |
18624 KB |
Output is correct |
22 |
Correct |
88 ms |
13816 KB |
Output is correct |
23 |
Correct |
109 ms |
19248 KB |
Output is correct |
24 |
Correct |
112 ms |
19184 KB |
Output is correct |
25 |
Correct |
114 ms |
19456 KB |
Output is correct |
26 |
Correct |
103 ms |
20292 KB |
Output is correct |
27 |
Correct |
94 ms |
19792 KB |
Output is correct |
28 |
Correct |
110 ms |
18980 KB |
Output is correct |
29 |
Correct |
108 ms |
20480 KB |
Output is correct |
30 |
Correct |
88 ms |
17868 KB |
Output is correct |
31 |
Correct |
110 ms |
19400 KB |
Output is correct |
32 |
Correct |
105 ms |
18656 KB |
Output is correct |
33 |
Correct |
110 ms |
19436 KB |
Output is correct |
34 |
Correct |
113 ms |
19320 KB |
Output is correct |
35 |
Correct |
101 ms |
17756 KB |
Output is correct |
36 |
Correct |
46 ms |
17480 KB |
Output is correct |
37 |
Correct |
116 ms |
20296 KB |
Output is correct |
38 |
Correct |
114 ms |
19344 KB |
Output is correct |
39 |
Correct |
112 ms |
21244 KB |
Output is correct |