#include "sorting.h"\
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "/home/ioi/codes/ioi15_d2/debug.h"
#else
#define debug(...) void(37)
#endif
template<typename T>
vector<T> inverse_fuck(T* a, int N) {
vector<T> res(N);
for (int i = 0; i < N; ++i) {
res[i] = a[i];
}
return res;
}
int findSwapPairs(int N, int __S[], int M, int __X[], int __Y[], int __P[], int __Q[]) {
auto S = inverse_fuck(__S, N);
auto X = inverse_fuck(__X, M);
auto Y = inverse_fuck(__Y, M);
vector<int> P(M), Q(M);
debug(X, Y);
auto Check = [&](int m) {
debug(m);
auto a = S;
for (int i = 0; i < m; ++i) {
swap(a[X[i]], a[Y[i]]);
}
debug(a);
vector<bool> vis(N);
vector<array<int, 2>> swaps;
for (int i = 0; i < N; ++i) {
if (vis[i]) {
continue;
}
int v = i;
while (a[v] != i) {
swaps.push_back({v, a[v]});
v = a[v];
vis[v] = true;
}
}
debug(swaps);
int moves = int(swaps.size());
if (moves > m) {
return false;
}
a = S;
vector<int> inv(N);
for (int i = 0; i < N; ++i) {
inv[a[i]] = i;
}
auto Move = [&](int x, int y) {
swap(inv[a[x]], inv[a[y]]);
swap(a[x], a[y]);
};
for (int i = 0; i < moves; ++i) {
Move(X[i], Y[i]);
P[i] = inv[swaps[i][0]];
Q[i] = inv[swaps[i][1]];
Move(P[i], Q[i]);
}
while (moves < m) {
P[moves] = Q[moves] = 0;
++moves;
}
return true;
};
int low = 0, high = M;
while (low < high) {
int mid = (low + high) >> 1;
if (!Check(mid)) {
low = mid + 1;
} else {
high = mid;
}
}
debug(low);
int ans = !Check(low) ? -1 : low;
if (ans != -1) {
for (int i = 0; i < ans; ++i) {
__P[i] = P[i];
__Q[i] = Q[i];
swap(S[X[i]], S[Y[i]]);
debug(S);
swap(S[P[i]], S[Q[i]]);
debug(S);
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 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 |
296 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 |
308 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
300 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 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 |
296 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 |
308 KB |
Output is correct |
18 |
Correct |
0 ms |
212 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 |
724 KB |
Output is correct |
22 |
Correct |
1 ms |
724 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
724 KB |
Output is correct |
25 |
Correct |
1 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
496 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
568 KB |
Output is correct |
11 |
Correct |
2 ms |
500 KB |
Output is correct |
12 |
Correct |
2 ms |
500 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
496 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
568 KB |
Output is correct |
11 |
Correct |
2 ms |
500 KB |
Output is correct |
12 |
Correct |
2 ms |
500 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
164 ms |
25964 KB |
Output is correct |
16 |
Correct |
167 ms |
26556 KB |
Output is correct |
17 |
Correct |
193 ms |
27876 KB |
Output is correct |
18 |
Correct |
64 ms |
26548 KB |
Output is correct |
19 |
Correct |
137 ms |
28132 KB |
Output is correct |
20 |
Correct |
140 ms |
28844 KB |
Output is correct |
21 |
Correct |
136 ms |
28744 KB |
Output is correct |
22 |
Correct |
178 ms |
23316 KB |
Output is correct |
23 |
Correct |
219 ms |
28824 KB |
Output is correct |
24 |
Correct |
198 ms |
28460 KB |
Output is correct |
25 |
Correct |
180 ms |
28108 KB |
Output is correct |
26 |
Correct |
196 ms |
27580 KB |
Output is correct |
27 |
Correct |
129 ms |
26812 KB |
Output is correct |
28 |
Correct |
185 ms |
28428 KB |
Output is correct |
29 |
Correct |
211 ms |
28064 KB |
Output is correct |
30 |
Correct |
101 ms |
26920 KB |
Output is correct |
31 |
Correct |
180 ms |
28636 KB |
Output is correct |
32 |
Correct |
166 ms |
27916 KB |
Output is correct |
33 |
Correct |
183 ms |
28444 KB |
Output is correct |
34 |
Correct |
180 ms |
28336 KB |
Output is correct |
35 |
Correct |
149 ms |
27628 KB |
Output is correct |
36 |
Correct |
54 ms |
26020 KB |
Output is correct |
37 |
Correct |
183 ms |
29324 KB |
Output is correct |
38 |
Correct |
190 ms |
28112 KB |
Output is correct |
39 |
Correct |
183 ms |
28300 KB |
Output is correct |