#include "sorting.h"
#include<bits/stdc++.h>
using namespace std;
int n;
int a[200005];
int b[200005];
int prebroj()
{
int ans=n;
for(int i=0; i<n; i++)
{
if(!b[i]) continue;
ans--;
int t=b[i];
b[i]=0;
while(b[t]!=0)
{
int t2=b[t];
b[t]=0;
t=t2;
}
}
return ans;
}
int sp[2][200005], brsp=0;
int pos[200005];
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[])
{
n=N;
for(int i=0; i<n; i++) a[i]=S[i];
int le=0, ri=M;
while(ri-le>1)
{
int mid=(le+ri)/2;
for(int i=0; i<n; i++) b[i]=a[i];
for(int j=0; j<mid; j++) swap(b[X[j]], b[Y[j]]);
if(prebroj()<=mid) ri=mid;
else le=mid;
}
for(int i=0; i<n; i++) b[i]=a[i];
for(int j=0; j<ri; j++) swap(b[X[j]], b[Y[j]]);
for(int i=0; i<n; i++) pos[b[i]]=i;
for(int i=0; i<n; i++)
{
int t=b[i];
if(t==i+1) continue;
sp[0][brsp]=t;
sp[1][brsp]=i+1;
swap(b[i], b[pos[i+1]]);
pos[b[i]]=pos[i+1];
pos[i+1]=i+1;
}
for(int i=0; i<n; i++) pos[a[i]]=i;
for(int i=0; i<brsp; i++)
{
P[i]=pos[sp[0][i]];
Q[i]=pos[sp[1][i]];
swap(pos[sp[0][i]], pos[sp[1][i]]);
swap(a[pos[sp[0][i]]], a[pos[sp[1][i]]]);
swap(a[X[i]], a[Y[i]]);
swap(pos[a[X[i]]], pos[a[Y[i]]]);
}
return ri;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |