// In the name of Allah
#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef complex<ld> cld;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define kill(x) cout << x << '\n', exit(0)
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, q;
const int maxn = 2e5 + 7;
int A1[maxn], A2[maxn];
int ind[maxn], indx[maxn];
int mark[maxn]; vector<pii> ls;
void dfs(int v) {
mark[v] = 1; int u = A1[v];
if (!mark[u]) dfs(u);
}
bool ok(int n, int S[], int q, int X[], int Y[]) {
for (int i = 0; i < n; i++) A1[i] = S[i];
for (int i = 0; i < q; i++) {
int i1 = X[i], i2 = Y[i];
swap(A1[i1], A1[i2]);
}
fill(mark, mark + n, 0); int c = 0;
for (int i = 0; i < n; i++) {
if (!mark[i]) {
dfs(i); c++;
}
}
return (n - c <= q);
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int l = -1, r = M;
while (r - l > 1) {
int mid = (l + r) / 2;
if (ok(N, S, mid, X, Y)) r = mid;
else l = mid;
}
n = N; q = r;
for (int i = 0; i < n; i++) {
A1[i] = S[i]; A2[i] = S[i];
ind[S[i]] = i; indx[S[i]] = i;
}
for (int i = 0; i < q; i++) {
int i1 = X[i], i2 = Y[i];
P[i] = 0; Q[i] = 0;
swap(A2[i1], A2[i2]);
swap(indx[A2[i1]], indx[A2[i2]]);
}
ls.clear();
for (int i = 0; i < n; i++) {
if (A2[i] == i) continue;
int i1 = i, i2 = indx[i];
ls.pb(Mp(A2[i1], A2[i2]));
swap(A2[i1], A2[i2]);
swap(indx[A2[i1]], indx[A2[i2]]);
}
for (int i = 0; i < q; i++) {
int i1 = X[i], i2 = Y[i];
swap(A1[i1], A1[i2]);
swap(ind[A1[i1]], ind[A1[i2]]);
if (i < len(ls)) {
int x1 = ls[i].F, x2 = ls[i].S;
P[i] = ind[x1]; Q[i] = ind[x2];
}
i1 = P[i], i2 = Q[i];
swap(A1[i1], A1[i2]);
swap(ind[A1[i1]], ind[A1[i2]]);
}
return q;
}
Compilation message
sorting.cpp: In function 'bool ok(int, int*, int, int*, int*)':
sorting.cpp:37:29: warning: declaration of 'q' shadows a global declaration [-Wshadow]
37 | bool ok(int n, int S[], int q, int X[], int Y[]) {
| ~~~~^
sorting.cpp:26:8: note: shadowed declaration is here
26 | int n, q;
| ^
sorting.cpp:37:13: warning: declaration of 'n' shadows a global declaration [-Wshadow]
37 | bool ok(int n, int S[], int q, int X[], int Y[]) {
| ~~~~^
sorting.cpp:26:5: note: shadowed declaration is here
26 | int n, q;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 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 |
392 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
392 KB |
Output is correct |
18 |
Correct |
0 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 |
720 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
720 KB |
Output is correct |
27 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
528 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
608 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
528 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
608 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
113 ms |
21500 KB |
Output is correct |
16 |
Correct |
131 ms |
22152 KB |
Output is correct |
17 |
Correct |
141 ms |
23180 KB |
Output is correct |
18 |
Correct |
52 ms |
17120 KB |
Output is correct |
19 |
Correct |
109 ms |
20932 KB |
Output is correct |
20 |
Correct |
132 ms |
21568 KB |
Output is correct |
21 |
Correct |
122 ms |
21704 KB |
Output is correct |
22 |
Correct |
125 ms |
18544 KB |
Output is correct |
23 |
Correct |
131 ms |
24060 KB |
Output is correct |
24 |
Correct |
150 ms |
23660 KB |
Output is correct |
25 |
Correct |
148 ms |
23536 KB |
Output is correct |
26 |
Correct |
116 ms |
21564 KB |
Output is correct |
27 |
Correct |
105 ms |
20732 KB |
Output is correct |
28 |
Correct |
142 ms |
23444 KB |
Output is correct |
29 |
Correct |
125 ms |
22688 KB |
Output is correct |
30 |
Correct |
74 ms |
19888 KB |
Output is correct |
31 |
Correct |
130 ms |
23380 KB |
Output is correct |
32 |
Correct |
125 ms |
23284 KB |
Output is correct |
33 |
Correct |
135 ms |
23852 KB |
Output is correct |
34 |
Correct |
124 ms |
23540 KB |
Output is correct |
35 |
Correct |
106 ms |
20688 KB |
Output is correct |
36 |
Correct |
40 ms |
18384 KB |
Output is correct |
37 |
Correct |
136 ms |
24512 KB |
Output is correct |
38 |
Correct |
126 ms |
23460 KB |
Output is correct |
39 |
Correct |
131 ms |
23488 KB |
Output is correct |