#include <bits/stdc++.h>
#include "sorting.h"
using namespace std;
bool check(int n, int xs[], int k, int ss1[], int ss2[], int ans1[] = nullptr, int ans2[] = nullptr) {
vector<int> ideal(n), idealrev(n), ys(n), ysrev(n);
for (int i = 0; i < n; ++i)
ideal[i] = idealrev[i] = ysrev[ys[i] = xs[i]] = i;
for (int i = k-1; i >= 0; --i) {
swap(ideal[ss1[i]], ideal[ss2[i]]);
swap(idealrev[ideal[ss1[i]]], idealrev[ideal[ss2[i]]]);
}
int lst = 0;
for (int i = 0; i < k; ++i) {
swap(ideal[ss1[i]], ideal[ss2[i]]);
swap(idealrev[ideal[ss1[i]]], idealrev[ideal[ss2[i]]]);
swap(ys[ss1[i]], ys[ss2[i]]);
swap(ysrev[ys[ss1[i]]], ysrev[ys[ss2[i]]]);
// cerr << "-> " << lst << " " << idealrev[lst] << " " << ysrev[lst] << "\n";
while (lst < n && idealrev[lst] == ysrev[lst]) ++lst;
if (lst < n) {
int a = ysrev[lst], b = idealrev[lst];
swap(ys[a], ys[b]);
swap(ysrev[ys[a]], ysrev[ys[b]]);
if (ans1 != nullptr) { ans1[i] = a; ans2[i] = b; }
}
else if (ans1 != nullptr) { ans1[i] = 0; ans2[i] = 0; }
}
for (int i = 0; i < n; ++i)
if (ys[i] != i) return false;
// cerr << k << "TRUE\n";
return true;
}
int findSwapPairs(int n, int xs[], int m, int ss1[], int ss2[], int ans1[], int ans2[]) {
int l = 0, r = m;
while (l < r) {
int mid = (l + r)/2;
if (check(n, xs, mid, ss1, ss2)) r = mid;
else l = mid + 1;
}
check(n, xs, r, ss1, ss2, ans1, ans2);
return r;
}
# |
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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 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 |
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 |
348 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 |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 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 |
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 |
348 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 |
1 ms |
348 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 |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
568 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 |
580 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
528 KB |
Output is correct |
15 |
Correct |
157 ms |
16848 KB |
Output is correct |
16 |
Correct |
180 ms |
17572 KB |
Output is correct |
17 |
Correct |
209 ms |
19760 KB |
Output is correct |
18 |
Correct |
78 ms |
16448 KB |
Output is correct |
19 |
Correct |
151 ms |
19820 KB |
Output is correct |
20 |
Correct |
158 ms |
20276 KB |
Output is correct |
21 |
Correct |
163 ms |
18628 KB |
Output is correct |
22 |
Correct |
153 ms |
13552 KB |
Output is correct |
23 |
Correct |
191 ms |
19076 KB |
Output is correct |
24 |
Correct |
213 ms |
18876 KB |
Output is correct |
25 |
Correct |
205 ms |
18648 KB |
Output is correct |
26 |
Correct |
159 ms |
18672 KB |
Output is correct |
27 |
Correct |
147 ms |
18120 KB |
Output is correct |
28 |
Correct |
215 ms |
18940 KB |
Output is correct |
29 |
Correct |
197 ms |
18312 KB |
Output is correct |
30 |
Correct |
118 ms |
17800 KB |
Output is correct |
31 |
Correct |
202 ms |
18764 KB |
Output is correct |
32 |
Correct |
169 ms |
18308 KB |
Output is correct |
33 |
Correct |
220 ms |
18976 KB |
Output is correct |
34 |
Correct |
191 ms |
18728 KB |
Output is correct |
35 |
Correct |
154 ms |
17728 KB |
Output is correct |
36 |
Correct |
64 ms |
17688 KB |
Output is correct |
37 |
Correct |
214 ms |
19636 KB |
Output is correct |
38 |
Correct |
205 ms |
18596 KB |
Output is correct |
39 |
Correct |
210 ms |
18728 KB |
Output is correct |