#include<bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#include "sorting.h"
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
auto check = [&](int R) {
vector<int> s(N), where(N), where_f(N);
REP(i, N) s[i] = S[i];
auto f = s;
REP(i, R) swap(f[X[i]], f[Y[i]]);
REP(i, N) {
where[S[i]] = i;
where_f[f[i]] = i;
}
int j = 0;
REP(i, R) P[i] = Q[i] = 0;
REP(i, R) {
int &p = s[X[i]], &q = s[Y[i]];
swap(p, q);
swap(where[p], where[q]);
while(j < N) {
int &a = where[j], &x = where_f[j];
int &b = where[f[j]], &y = where_f[f[j]];
j++;
if(a == b) continue;
P[i] = a, Q[i] = b;
swap(a, b), swap(s[a], s[b]);
swap(x, y), swap(f[x], f[y]);
break;
}
}
REP(i, N) if(i != s[i]) return false;
return true;
};
int l = 0, r = M;
while(l < r) {
int m = (l + r) / 2;
if(check(m))
r = m;
else
l = m + 1;
}
check(l);
return l;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
128 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
128 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
128 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
4 ms |
256 KB |
Output is correct |
4 |
Correct |
4 ms |
256 KB |
Output is correct |
5 |
Correct |
4 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
4 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
4 ms |
256 KB |
Output is correct |
19 |
Correct |
4 ms |
256 KB |
Output is correct |
20 |
Correct |
4 ms |
256 KB |
Output is correct |
21 |
Correct |
6 ms |
640 KB |
Output is correct |
22 |
Correct |
6 ms |
640 KB |
Output is correct |
23 |
Correct |
6 ms |
640 KB |
Output is correct |
24 |
Correct |
6 ms |
640 KB |
Output is correct |
25 |
Correct |
5 ms |
640 KB |
Output is correct |
26 |
Correct |
6 ms |
640 KB |
Output is correct |
27 |
Correct |
6 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Correct |
6 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Correct |
6 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
6 ms |
512 KB |
Output is correct |
8 |
Correct |
6 ms |
512 KB |
Output is correct |
9 |
Correct |
6 ms |
512 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
6 ms |
512 KB |
Output is correct |
12 |
Correct |
6 ms |
512 KB |
Output is correct |
13 |
Correct |
6 ms |
512 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
296 ms |
17744 KB |
Output is correct |
16 |
Correct |
315 ms |
18188 KB |
Output is correct |
17 |
Correct |
378 ms |
19204 KB |
Output is correct |
18 |
Correct |
141 ms |
18576 KB |
Output is correct |
19 |
Correct |
273 ms |
19784 KB |
Output is correct |
20 |
Correct |
289 ms |
20304 KB |
Output is correct |
21 |
Correct |
294 ms |
20216 KB |
Output is correct |
22 |
Correct |
287 ms |
14488 KB |
Output is correct |
23 |
Correct |
325 ms |
19888 KB |
Output is correct |
24 |
Correct |
392 ms |
19732 KB |
Output is correct |
25 |
Correct |
371 ms |
19456 KB |
Output is correct |
26 |
Correct |
272 ms |
20208 KB |
Output is correct |
27 |
Correct |
256 ms |
19864 KB |
Output is correct |
28 |
Correct |
371 ms |
19824 KB |
Output is correct |
29 |
Correct |
348 ms |
19544 KB |
Output is correct |
30 |
Correct |
203 ms |
19940 KB |
Output is correct |
31 |
Correct |
360 ms |
19900 KB |
Output is correct |
32 |
Correct |
323 ms |
19176 KB |
Output is correct |
33 |
Correct |
380 ms |
19612 KB |
Output is correct |
34 |
Correct |
360 ms |
19532 KB |
Output is correct |
35 |
Correct |
276 ms |
19396 KB |
Output is correct |
36 |
Correct |
123 ms |
19988 KB |
Output is correct |
37 |
Correct |
393 ms |
20264 KB |
Output is correct |
38 |
Correct |
371 ms |
19380 KB |
Output is correct |
39 |
Correct |
375 ms |
19476 KB |
Output is correct |