#include "sorting.h"
#include "bits/stdc++.h"
using namespace std;
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
if (is_sorted(S, S + N)) {
return 0;
}
auto check = [&](int mid) {
vector<int> finalArray(N);
for (int i = 0; i < N; ++i) {
finalArray[i] = S[i];
}
for (int i = 0; i < mid; ++i) {
swap(finalArray[X[i]], finalArray[Y[i]]);
}
vector<int> next(N);
vector<pair<int, int>> swaps;
for (int i = 0; i < N; ++i) {
next[i] = finalArray[i];
}
vector<bool> vis(N);
for (int i = 0; i < N; ++i) {
if (vis[i] || next[i] == i) continue;
int cur = i, nxt = next[i];
vis[i] = true;
while (nxt != i) {
vis[nxt] = true;
swaps.emplace_back(cur, nxt);
cur = nxt;
nxt = next[cur];
}
}
vector<int> idx(N);
vector<int> currentArray(N);
for (int i = 0; i < N; ++i) {
idx[S[i]] = i;
currentArray[i] = S[i];
}
auto swapp = [&](int x, int y) {
swap(idx[x], idx[y]);
swap(currentArray[idx[x]], currentArray[idx[y]]);
};
for (int i = 0; i < (int) swaps.size(); ++i) {
int x = currentArray[X[i]], y = currentArray[Y[i]];
swapp(x, y);
auto [p, q] = swaps[i];
P[i] = idx[p], Q[i] = idx[q];
swapp(p, q);
}
for (int i = (int) swaps.size(); i < mid; ++i) {
P[i] = Q[i] = 0;
}
return (int) swaps.size() <= mid;
};
int l = 0, r = M;
while (l < r) {
int mid = (l + r) / 2;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
check(l);
return l;
}
# |
Verdict |
Execution time |
Memory |
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 |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 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 |
444 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 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 |
444 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
504 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
592 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
600 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
448 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
444 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
448 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
444 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
183 ms |
19904 KB |
Output is correct |
16 |
Correct |
185 ms |
20284 KB |
Output is correct |
17 |
Correct |
204 ms |
22876 KB |
Output is correct |
18 |
Correct |
35 ms |
13396 KB |
Output is correct |
19 |
Correct |
180 ms |
22304 KB |
Output is correct |
20 |
Correct |
168 ms |
22708 KB |
Output is correct |
21 |
Correct |
169 ms |
21980 KB |
Output is correct |
22 |
Correct |
175 ms |
16632 KB |
Output is correct |
23 |
Correct |
205 ms |
23032 KB |
Output is correct |
24 |
Correct |
210 ms |
21760 KB |
Output is correct |
25 |
Correct |
207 ms |
21488 KB |
Output is correct |
26 |
Correct |
163 ms |
21728 KB |
Output is correct |
27 |
Correct |
142 ms |
20768 KB |
Output is correct |
28 |
Correct |
207 ms |
23048 KB |
Output is correct |
29 |
Correct |
192 ms |
22316 KB |
Output is correct |
30 |
Correct |
118 ms |
21164 KB |
Output is correct |
31 |
Correct |
193 ms |
22712 KB |
Output is correct |
32 |
Correct |
190 ms |
21456 KB |
Output is correct |
33 |
Correct |
207 ms |
21704 KB |
Output is correct |
34 |
Correct |
202 ms |
22756 KB |
Output is correct |
35 |
Correct |
168 ms |
20808 KB |
Output is correct |
36 |
Correct |
70 ms |
20000 KB |
Output is correct |
37 |
Correct |
208 ms |
22312 KB |
Output is correct |
38 |
Correct |
214 ms |
22388 KB |
Output is correct |
39 |
Correct |
210 ms |
22832 KB |
Output is correct |