#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), ws(N), wf(N);
REP(i, N) s[i] = S[i];
auto f = s;
REP(i, R) swap(f[X[i]], f[Y[i]]);
REP(i, N) {
ws[S[i]] = i;
wf[f[i]] = i;
}
int j = 0;
REP(i, R) {
int &p = s[X[i]], &q = s[Y[i]];
swap(p, q);
swap(ws[p], ws[q]);
while(j < N && j == f[j]) j++;
if(j < N) {
int &a = ws[j], &b = ws[f[j]], &x = wf[j], &y = wf[f[j]];
P[i] = a, Q[i] = b;
swap(a, b), swap(s[a], s[b]);
swap(x, y), swap(f[x], f[y]);
}
else P[i] = Q[i] = 0;
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
128 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
256 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
256 KB |
Output is correct |
9 |
Correct |
4 ms |
128 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
4 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 |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
256 KB |
Output is correct |
21 |
Correct |
8 ms |
512 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
512 KB |
Output is correct |
24 |
Correct |
5 ms |
512 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
6 ms |
512 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
7 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
7 ms |
384 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
6 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
384 KB |
Output is correct |
2 |
Correct |
7 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
6 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
512 KB |
Output is correct |
11 |
Correct |
7 ms |
384 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
6 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
317 ms |
10128 KB |
Output is correct |
16 |
Correct |
358 ms |
10292 KB |
Output is correct |
17 |
Correct |
422 ms |
10872 KB |
Output is correct |
18 |
Correct |
147 ms |
10524 KB |
Output is correct |
19 |
Correct |
291 ms |
11128 KB |
Output is correct |
20 |
Correct |
305 ms |
11492 KB |
Output is correct |
21 |
Correct |
305 ms |
11476 KB |
Output is correct |
22 |
Correct |
327 ms |
10984 KB |
Output is correct |
23 |
Correct |
375 ms |
11208 KB |
Output is correct |
24 |
Correct |
438 ms |
11192 KB |
Output is correct |
25 |
Correct |
425 ms |
10944 KB |
Output is correct |
26 |
Correct |
302 ms |
11352 KB |
Output is correct |
27 |
Correct |
274 ms |
11140 KB |
Output is correct |
28 |
Correct |
404 ms |
11088 KB |
Output is correct |
29 |
Correct |
384 ms |
10992 KB |
Output is correct |
30 |
Correct |
216 ms |
11192 KB |
Output is correct |
31 |
Correct |
397 ms |
11208 KB |
Output is correct |
32 |
Correct |
360 ms |
10836 KB |
Output is correct |
33 |
Correct |
421 ms |
11128 KB |
Output is correct |
34 |
Correct |
392 ms |
11152 KB |
Output is correct |
35 |
Correct |
305 ms |
10888 KB |
Output is correct |
36 |
Correct |
118 ms |
11380 KB |
Output is correct |
37 |
Correct |
442 ms |
11460 KB |
Output is correct |
38 |
Correct |
401 ms |
10944 KB |
Output is correct |
39 |
Correct |
438 ms |
11020 KB |
Output is correct |