#include<bits/stdc++.h>
#include "sorting.h"
using namespace std;
int n,m,cur[600005],def[600005],x[600005],y[600005];
int p[600005],q[600005],where[600005],cnt,ans;
void Swap(int A,int B) {
int tmp;
tmp=where[cur[A]];
where[cur[A]]=where[cur[B]];
where[cur[B]]=tmp;
tmp=cur[A];
cur[A]=cur[B];
cur[B]=tmp;
}
int param(int s,int e) {
int piv=(s+e)/2,i;
for(i=0;i<n;i++) {
cur[i]=def[i];
where[cur[i]]=i;
}
for(i=0;i<piv;i++) {
Swap(x[i],y[i]);
}
cnt=0;
for(i=0;i<n;i++) {
while(cur[i]!=i) {
p[cnt]=cur[i];
q[cnt]=cur[cur[i]];
Swap(i,cur[i]);
cnt++;
}
}
if(s==e)return s;
if(cnt<=piv) return param(s,piv);
return param(piv+1,e);
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int i; n=N; m=M;
for(i=0;i<n;i++) def[i]=S[i];
for(i=0;i<m;i++) {
x[i]=X[i];
y[i]=Y[i];
}
ans=param(0,m);
for(i=0;i<n;i++) {
cur[i]=def[i];
where[cur[i]]=i;
}
for(i=0;i<cnt;i++) {
Swap(x[i],y[i]);
P[i]=where[p[i]]; Q[i]=where[q[i]];
Swap(where[p[i]],where[q[i]]);
}
for(i=cnt;i<ans;i++) {P[i]=0;Q[i]=0;}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
512 KB |
Output is correct |
12 |
Correct |
3 ms |
512 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
3 ms |
512 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
3 ms |
640 KB |
Output is correct |
22 |
Correct |
4 ms |
640 KB |
Output is correct |
23 |
Correct |
3 ms |
640 KB |
Output is correct |
24 |
Correct |
3 ms |
640 KB |
Output is correct |
25 |
Correct |
4 ms |
640 KB |
Output is correct |
26 |
Correct |
3 ms |
640 KB |
Output is correct |
27 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
11 |
Correct |
4 ms |
512 KB |
Output is correct |
12 |
Correct |
4 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
4 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
4 ms |
512 KB |
Output is correct |
7 |
Correct |
4 ms |
512 KB |
Output is correct |
8 |
Correct |
3 ms |
512 KB |
Output is correct |
9 |
Correct |
5 ms |
512 KB |
Output is correct |
10 |
Correct |
4 ms |
512 KB |
Output is correct |
11 |
Correct |
4 ms |
512 KB |
Output is correct |
12 |
Correct |
4 ms |
512 KB |
Output is correct |
13 |
Correct |
4 ms |
512 KB |
Output is correct |
14 |
Correct |
3 ms |
512 KB |
Output is correct |
15 |
Correct |
294 ms |
15776 KB |
Output is correct |
16 |
Correct |
336 ms |
16112 KB |
Output is correct |
17 |
Correct |
405 ms |
17364 KB |
Output is correct |
18 |
Correct |
83 ms |
13432 KB |
Output is correct |
19 |
Correct |
174 ms |
15864 KB |
Output is correct |
20 |
Correct |
199 ms |
16548 KB |
Output is correct |
21 |
Correct |
189 ms |
16476 KB |
Output is correct |
22 |
Correct |
344 ms |
17112 KB |
Output is correct |
23 |
Correct |
321 ms |
17528 KB |
Output is correct |
24 |
Correct |
355 ms |
17472 KB |
Output is correct |
25 |
Correct |
351 ms |
17660 KB |
Output is correct |
26 |
Correct |
235 ms |
15864 KB |
Output is correct |
27 |
Correct |
238 ms |
14968 KB |
Output is correct |
28 |
Correct |
372 ms |
17180 KB |
Output is correct |
29 |
Correct |
367 ms |
17132 KB |
Output is correct |
30 |
Correct |
217 ms |
14148 KB |
Output is correct |
31 |
Correct |
325 ms |
17528 KB |
Output is correct |
32 |
Correct |
286 ms |
17016 KB |
Output is correct |
33 |
Correct |
322 ms |
17784 KB |
Output is correct |
34 |
Correct |
337 ms |
17400 KB |
Output is correct |
35 |
Correct |
194 ms |
15736 KB |
Output is correct |
36 |
Correct |
50 ms |
12792 KB |
Output is correct |
37 |
Correct |
293 ms |
18552 KB |
Output is correct |
38 |
Correct |
245 ms |
17528 KB |
Output is correct |
39 |
Correct |
282 ms |
17980 KB |
Output is correct |