#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifndef WAIMAI
#include "sorting.h"
#endif
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
const int SIZE = 2e5 + 5;
int n;
int a[SIZE], p[SIZE];
bool vs[SIZE];
bool ok(int r, int S[], int X[], int Y[]) {
FOR (i, 0, n - 1) a[i] = S[i], vs[i] = 0;
FOR (i, 0, r - 1) swap(a[X[i]], a[Y[i]]);
int cnt = n;
FOR (i, 0, n - 1) if (!vs[i]) {
cnt--;
for (int cur = i; !vs[cur]; cur = a[cur]) vs[cur] = 1;
}
return cnt <= r;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n = N;
int l = 0, r = M;
while (l < r) {
int mid = (l + r) / 2;
if (ok(mid, S, X, Y)) r = mid;
else l = mid + 1;
}
FOR (i, 0, n - 1) a[i] = S[i], vs[i] = 0;
FOR (i, 0, r - 1) swap(a[X[i]], a[Y[i]]);
int sz = 0;
FOR (i, 0, n - 1) if (!vs[i]) {
int last = -1, cur = i;
while (!vs[cur]) {
vs[cur] = 1;
if (last != -1) P[sz] = last, Q[sz] = cur, sz++;
last = cur;
cur = a[cur];
}
}
while (sz < r) P[sz] = Q[sz] = 0, sz++;
FOR (i, 0, n - 1) p[S[i]] = i;
FOR (i, 0, r - 1) {
swap(p[S[X[i]]], p[S[Y[i]]]);
swap(S[X[i]], S[Y[i]]);
int a = P[i], b = Q[i];
P[i] = p[a], Q[i] = p[b];
swap(p[S[P[i]]], p[S[Q[i]]]);
swap(S[P[i]], S[Q[i]]);
}
return r;
}
/*
in1
5
4 3 2 1 0
6
0 1
1 2
2 3
3 4
0 1
1 2
out1
3
0 4
1 3
3 4
in2
5
3 0 4 2 1
5
1 1
4 0
2 3
1 4
0 4
out2
3
1 4
4 2
2 2
*/
#ifdef WAIMAI
int main() {
int N, M;
cin >> N;
int *S = (int*)malloc(sizeof(int) * (unsigned int)N);
for (int i = 0; i < N; ++i) cin >> S[i];
cin >> M;
int *X = (int*)malloc(sizeof(int) * (unsigned int)M), *Y = (int*)malloc(sizeof(int) * (unsigned int)M);
for (int i = 0; i < M; ++i) {
cin >> X[i];
cin >> Y[i];
}
int *P = (int*)malloc(sizeof(int) * (unsigned int)M), *Q = (int*)malloc(sizeof(int) * (unsigned int)M);
int ans = findSwapPairs(N, S, M, X, Y, P, Q);
cout << ans << '\n';
for (int i = 0; i < ans; ++i) cout << P[i] << ' ' << Q[i] << '\n';
return 0;
}
#endif
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:66:13: warning: declaration of 'a' shadows a global declaration [-Wshadow]
66 | int a = P[i], b = Q[i];
| ^
sorting.cpp:27:5: note: shadowed declaration is here
27 | int a[SIZE], p[SIZE];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
308 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 |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
308 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 |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
308 KB |
Output is correct |
19 |
Correct |
1 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 |
452 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
129 ms |
17920 KB |
Output is correct |
16 |
Correct |
134 ms |
18540 KB |
Output is correct |
17 |
Correct |
157 ms |
19404 KB |
Output is correct |
18 |
Correct |
62 ms |
15028 KB |
Output is correct |
19 |
Correct |
137 ms |
17796 KB |
Output is correct |
20 |
Correct |
154 ms |
18380 KB |
Output is correct |
21 |
Correct |
139 ms |
18424 KB |
Output is correct |
22 |
Correct |
125 ms |
14824 KB |
Output is correct |
23 |
Correct |
145 ms |
20224 KB |
Output is correct |
24 |
Correct |
154 ms |
19832 KB |
Output is correct |
25 |
Correct |
159 ms |
19592 KB |
Output is correct |
26 |
Correct |
129 ms |
18372 KB |
Output is correct |
27 |
Correct |
123 ms |
17792 KB |
Output is correct |
28 |
Correct |
148 ms |
19784 KB |
Output is correct |
29 |
Correct |
147 ms |
19124 KB |
Output is correct |
30 |
Correct |
85 ms |
17268 KB |
Output is correct |
31 |
Correct |
161 ms |
19584 KB |
Output is correct |
32 |
Correct |
147 ms |
19300 KB |
Output is correct |
33 |
Correct |
147 ms |
19904 KB |
Output is correct |
34 |
Correct |
147 ms |
19836 KB |
Output is correct |
35 |
Correct |
125 ms |
17552 KB |
Output is correct |
36 |
Correct |
47 ms |
16104 KB |
Output is correct |
37 |
Correct |
150 ms |
19800 KB |
Output is correct |
38 |
Correct |
147 ms |
19608 KB |
Output is correct |
39 |
Correct |
152 ms |
19020 KB |
Output is correct |