#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 = 1, 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;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
31 ms |
476 KB |
Output is correct |
11 |
Correct |
26 ms |
344 KB |
Output is correct |
12 |
Correct |
27 ms |
476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
27 ms |
504 KB |
Output is correct |
4 |
Correct |
25 ms |
488 KB |
Output is correct |
5 |
Correct |
28 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
31 ms |
476 KB |
Output is correct |
11 |
Correct |
26 ms |
344 KB |
Output is correct |
12 |
Correct |
27 ms |
476 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
27 ms |
504 KB |
Output is correct |
16 |
Correct |
25 ms |
488 KB |
Output is correct |
17 |
Correct |
28 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
140 ms |
956 KB |
Output is correct |
22 |
Correct |
144 ms |
852 KB |
Output is correct |
23 |
Correct |
147 ms |
852 KB |
Output is correct |
24 |
Correct |
142 ms |
748 KB |
Output is correct |
25 |
Correct |
120 ms |
600 KB |
Output is correct |
26 |
Correct |
125 ms |
848 KB |
Output is correct |
27 |
Correct |
145 ms |
700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
852 KB |
Output is correct |
2 |
Correct |
201 ms |
852 KB |
Output is correct |
3 |
Correct |
199 ms |
944 KB |
Output is correct |
4 |
Incorrect |
45 ms |
604 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
852 KB |
Output is correct |
2 |
Correct |
201 ms |
852 KB |
Output is correct |
3 |
Correct |
199 ms |
944 KB |
Output is correct |
4 |
Incorrect |
45 ms |
604 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |