#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
int *s, *x, *y, *p, *q;
int ans;
int n, m;
int A[200010], B[200010], posB[200010], F[200010], posF[200010];
//현재 에르맥, 현재 수열, 현재 수열의 pos, 결과 수열, 결과 수열의 pos
int seq[600010][2], tp;
bool canSwap(int l)
{
int i; tp=0;
for (i=0; i<n; i++) A[i]=i, B[i]=s[i], posB[s[i]]=i, F[i]=s[i], posF[s[i]]=i;
for (i=0; i<l; i++) {
swap(F[x[i]], F[y[i]]);
swap(posF[F[x[i]]], posF[F[y[i]]]);
}
for (i=l-1; i>=0; i--) swap(A[x[i]], A[y[i]]);
while (tp<n&&F[tp]==tp) tp++;
for (i=0; i<l; i++) {
swap(A[x[i]], A[y[i]]);
swap(B[x[i]], B[y[i]]);
swap(posB[B[x[i]]], posB[B[y[i]]]);
int i1=F[tp], i2=F[posF[tp]];
swap(F[tp], F[posF[tp]]);
swap(posF[i1], posF[i2]);
seq[i][0]=posB[i1], seq[i][1]=posB[i2];
swap(B[posB[i1]], B[posB[i2]]);
swap(posB[i1], posB[i2]);
//for (int j=0; j<n; j++) printf("%d ", A[j]); puts("");
//for (int j=0; j<n; j++) printf("%d ", B[j]); puts("");
//for (int j=0; j<n; j++) printf("%d ", F[j]); puts("");
//puts("");
while (tp<n&&F[tp]==tp) tp++;
if (tp>=n) break;
}
if (tp>=n) {
//printf("%d\n", l);
for (int j=0; j<=i; j++) p[j]=seq[j][0], q[j]=seq[j][1];
for (int j=i+1; j<l; j++) p[j]=q[j]=1;
ans=l;
return true;
}
return false;
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
s=S; x=X; y=Y; p=P; q=Q; n=N; m=M;
int st=0, re=M;
while (st<re) {
int md=(st+re)/2;
if (canSwap(md)) re=md;
else st=md+1;
//puts("");
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
504 KB |
Output is correct |
20 |
Correct |
2 ms |
376 KB |
Output is correct |
21 |
Correct |
3 ms |
632 KB |
Output is correct |
22 |
Correct |
3 ms |
632 KB |
Output is correct |
23 |
Correct |
3 ms |
628 KB |
Output is correct |
24 |
Correct |
3 ms |
636 KB |
Output is correct |
25 |
Correct |
3 ms |
632 KB |
Output is correct |
26 |
Correct |
3 ms |
632 KB |
Output is correct |
27 |
Correct |
3 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
508 KB |
Output is correct |
7 |
Correct |
4 ms |
820 KB |
Output is correct |
8 |
Correct |
4 ms |
608 KB |
Output is correct |
9 |
Correct |
4 ms |
504 KB |
Output is correct |
10 |
Correct |
4 ms |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3 ms |
504 KB |
Output is correct |
6 |
Correct |
3 ms |
508 KB |
Output is correct |
7 |
Correct |
4 ms |
820 KB |
Output is correct |
8 |
Correct |
4 ms |
608 KB |
Output is correct |
9 |
Correct |
4 ms |
504 KB |
Output is correct |
10 |
Correct |
4 ms |
504 KB |
Output is correct |
11 |
Correct |
4 ms |
504 KB |
Output is correct |
12 |
Correct |
3 ms |
632 KB |
Output is correct |
13 |
Correct |
4 ms |
632 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
440 ms |
22208 KB |
Output is correct |
16 |
Correct |
544 ms |
22648 KB |
Output is correct |
17 |
Correct |
678 ms |
23928 KB |
Output is correct |
18 |
Correct |
128 ms |
20648 KB |
Output is correct |
19 |
Correct |
321 ms |
23072 KB |
Output is correct |
20 |
Correct |
345 ms |
23780 KB |
Output is correct |
21 |
Correct |
345 ms |
23800 KB |
Output is correct |
22 |
Correct |
481 ms |
19192 KB |
Output is correct |
23 |
Correct |
491 ms |
24824 KB |
Output is correct |
24 |
Correct |
706 ms |
24516 KB |
Output is correct |
25 |
Correct |
756 ms |
24012 KB |
Output is correct |
26 |
Correct |
502 ms |
23148 KB |
Output is correct |
27 |
Correct |
435 ms |
22364 KB |
Output is correct |
28 |
Correct |
673 ms |
24312 KB |
Output is correct |
29 |
Correct |
614 ms |
23748 KB |
Output is correct |
30 |
Correct |
413 ms |
21624 KB |
Output is correct |
31 |
Correct |
625 ms |
24248 KB |
Output is correct |
32 |
Correct |
488 ms |
23828 KB |
Output is correct |
33 |
Correct |
671 ms |
24316 KB |
Output is correct |
34 |
Correct |
607 ms |
24400 KB |
Output is correct |
35 |
Correct |
366 ms |
22624 KB |
Output is correct |
36 |
Correct |
78 ms |
20600 KB |
Output is correct |
37 |
Correct |
599 ms |
25052 KB |
Output is correct |
38 |
Correct |
512 ms |
24000 KB |
Output is correct |
39 |
Correct |
552 ms |
24228 KB |
Output is correct |