#include "sorting.h"
#include <algorithm>
using namespace std;
int a[200002],location[200002];
int where[200002],last[200002];
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
int i,n=N,R;
int left,right;
int cnt;
left=0;
right=M;
while(1){
R=(left+right)/2;
for(i=0;i<n;i++){
a[i]=S[i];
location[a[i]]=i;
where[a[i]]=i;
}
for(i=0;i<R;i++){
location[a[X[i]]]=Y[i];
location[a[Y[i]]]=X[i];
swap(a[X[i]],a[Y[i]]);
}
cnt=-1;
for(i=0;i<n;i++) last[i]=a[i];
for(i=0;i<n;i++) a[i]=S[i];
for(i=0;i<n;i++){
if(location[i]==i) continue;
cnt++;
if(cnt>=R) break;
where[a[X[cnt]]]=Y[cnt];
where[a[Y[cnt]]]=X[cnt];
swap(a[X[cnt]],a[Y[cnt]]);
P[cnt]=where[i];
Q[cnt]=where[last[i]];
location[last[i]]=location[i];
location[i]=i;
where[a[P[cnt]]]=Q[cnt];
where[a[Q[cnt]]]=P[cnt];
swap(a[P[cnt]],a[Q[cnt]]);
}
if(left==right) break;
if(cnt>=R){
left=R+1;
}else{
right=R;
}
}
for(i=cnt+1;i<R;i++) P[i]=Q[i]=0;
return R;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Incorrect |
2 ms |
256 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Incorrect |
2 ms |
256 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
256 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
256 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
256 KB |
Output is correct |
5 |
Incorrect |
2 ms |
256 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |