#include "sorting.h"
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pb push_back
#define mk make_pair
#define all(s) s.begin(), s.end()
using namespace std;
const int N = 2e5 + 5;
int sz, cnt, res, u[N], a[N], pos[N], asd[N];
void dfs (int v) {
cnt++;
u[v] = 1;
if (u[ a[v] ] == 0)
dfs(a[v]);
}
int findSwapPairs(int n, int b[], int m, int x[], int y[], int p[], int q[]) {
bool fl = 1;
for (int i = 0; i < n; i++) {
a[i] = b[i];
if (a[i] != i)
fl = 0;
}
if (fl)
return 0;
int l = 0, r = m;
while (r - l > 1) {
int md = (l + r) >> 1;
for (int i = 0; i < n; i++)
a[i] = b[i];
for (int i = 0; i < md; i++) {
swap( a[ x[i] ], a[ y[i] ] );
}
res = 0;
memset(u, 0, sizeof(u));
for (int j = 0; j < n; j++) {
if (u[j]) continue;
cnt = 0; dfs(j);
res += cnt - 1;
}
if (res <= md)
r = md;
else
l = md;
}
for (int i = 0; i < n; i++)
a[i] = b[i];
for (int i = 0; i < r; i++) {
swap( a[ x[i] ], a[ y[i] ] );
}
for (int j = 0; j < n; j++)
pos[b[j]] = j, asd[a[j]] = j;
int k = 0;
for (int j = 0; j < n; j++) {
if (a[j] == j) continue;
pos[ b[x[k]] ] = y[k];
pos[ b[y[k]] ] = x[k];
swap( b[x[k]], b[y[k]] );
p[k] = pos[a[j]], q[k] = pos[j];
swap( b[pos[ a[j] ]], b[pos[j]] );
swap( pos[ a[j] ], pos[j] );
swap(a[j], a[asd[j]]);
asd[a[asd[j]]] = asd[j];
k++;
}
return r;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1144 KB |
Output is correct |
6 |
Correct |
3 ms |
1144 KB |
Output is correct |
7 |
Correct |
3 ms |
1144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1144 KB |
Output is correct |
6 |
Correct |
3 ms |
1144 KB |
Output is correct |
7 |
Correct |
3 ms |
1144 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
1144 KB |
Output is correct |
11 |
Correct |
3 ms |
1144 KB |
Output is correct |
12 |
Correct |
7 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
1144 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1144 KB |
Output is correct |
6 |
Correct |
3 ms |
1144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
3 ms |
1144 KB |
Output is correct |
4 |
Correct |
3 ms |
1144 KB |
Output is correct |
5 |
Correct |
3 ms |
1144 KB |
Output is correct |
6 |
Correct |
3 ms |
1144 KB |
Output is correct |
7 |
Correct |
3 ms |
1144 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
3 ms |
1144 KB |
Output is correct |
11 |
Correct |
3 ms |
1144 KB |
Output is correct |
12 |
Correct |
7 ms |
1272 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
3 ms |
1144 KB |
Output is correct |
15 |
Correct |
3 ms |
1144 KB |
Output is correct |
16 |
Correct |
3 ms |
1144 KB |
Output is correct |
17 |
Correct |
3 ms |
1144 KB |
Output is correct |
18 |
Correct |
3 ms |
1144 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
4 ms |
1400 KB |
Output is correct |
22 |
Correct |
4 ms |
1504 KB |
Output is correct |
23 |
Correct |
4 ms |
1400 KB |
Output is correct |
24 |
Correct |
4 ms |
1272 KB |
Output is correct |
25 |
Correct |
4 ms |
1276 KB |
Output is correct |
26 |
Correct |
4 ms |
1272 KB |
Output is correct |
27 |
Correct |
4 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1272 KB |
Output is correct |
2 |
Correct |
4 ms |
1276 KB |
Output is correct |
3 |
Correct |
4 ms |
1272 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
1272 KB |
Output is correct |
6 |
Correct |
4 ms |
1272 KB |
Output is correct |
7 |
Correct |
5 ms |
1272 KB |
Output is correct |
8 |
Correct |
5 ms |
1272 KB |
Output is correct |
9 |
Correct |
4 ms |
1272 KB |
Output is correct |
10 |
Correct |
4 ms |
1272 KB |
Output is correct |
11 |
Correct |
4 ms |
1272 KB |
Output is correct |
12 |
Correct |
4 ms |
1272 KB |
Output is correct |
13 |
Correct |
5 ms |
1272 KB |
Output is correct |
14 |
Correct |
4 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1272 KB |
Output is correct |
2 |
Correct |
4 ms |
1276 KB |
Output is correct |
3 |
Correct |
4 ms |
1272 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
1272 KB |
Output is correct |
6 |
Correct |
4 ms |
1272 KB |
Output is correct |
7 |
Correct |
5 ms |
1272 KB |
Output is correct |
8 |
Correct |
5 ms |
1272 KB |
Output is correct |
9 |
Correct |
4 ms |
1272 KB |
Output is correct |
10 |
Correct |
4 ms |
1272 KB |
Output is correct |
11 |
Correct |
4 ms |
1272 KB |
Output is correct |
12 |
Correct |
4 ms |
1272 KB |
Output is correct |
13 |
Correct |
5 ms |
1272 KB |
Output is correct |
14 |
Correct |
4 ms |
1272 KB |
Output is correct |
15 |
Correct |
228 ms |
12024 KB |
Output is correct |
16 |
Correct |
188 ms |
19904 KB |
Output is correct |
17 |
Correct |
259 ms |
20880 KB |
Output is correct |
18 |
Correct |
50 ms |
14200 KB |
Output is correct |
19 |
Correct |
170 ms |
19292 KB |
Output is correct |
20 |
Correct |
190 ms |
19964 KB |
Output is correct |
21 |
Correct |
182 ms |
19960 KB |
Output is correct |
22 |
Correct |
180 ms |
16376 KB |
Output is correct |
23 |
Correct |
197 ms |
21648 KB |
Output is correct |
24 |
Correct |
243 ms |
21368 KB |
Output is correct |
25 |
Correct |
225 ms |
21112 KB |
Output is correct |
26 |
Correct |
183 ms |
19832 KB |
Output is correct |
27 |
Correct |
159 ms |
19192 KB |
Output is correct |
28 |
Correct |
218 ms |
21112 KB |
Output is correct |
29 |
Correct |
213 ms |
20672 KB |
Output is correct |
30 |
Correct |
112 ms |
18552 KB |
Output is correct |
31 |
Correct |
223 ms |
21060 KB |
Output is correct |
32 |
Correct |
194 ms |
20864 KB |
Output is correct |
33 |
Correct |
225 ms |
21368 KB |
Output is correct |
34 |
Correct |
228 ms |
21408 KB |
Output is correct |
35 |
Correct |
170 ms |
19064 KB |
Output is correct |
36 |
Correct |
56 ms |
17528 KB |
Output is correct |
37 |
Correct |
249 ms |
21880 KB |
Output is correct |
38 |
Correct |
224 ms |
21112 KB |
Output is correct |
39 |
Correct |
225 ms |
21240 KB |
Output is correct |