#include "sorting.h"
#include <bits/stdc++.h>
using namespace std;
const int nx=2e5+5;
int s[nx], rs[nx], loc[nx], idx[nx], ridx[nx], n, p[nx], q[nx];
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
n=N;
int l=0, r=N-1;
while (l<r)
{
int md=(l+r)/2, cnt=0, cur=0;
for (int i=0; i<N; i++) idx[i]=i, ridx[i]=i, s[i]=S[i], rs[s[i]]=i;
for (int i=md-1; i>=0; i--) swap(idx[X[i]], idx[Y[i]]), swap(ridx[idx[X[i]]], ridx[idx[Y[i]]]);
//cout<<"debug "<<l<<' '<<r<<' '<<md<<'\n';
for (int i=0; i<md; i++)
{
swap(ridx[idx[X[i]]], ridx[idx[Y[i]]]);
swap(idx[X[i]], idx[Y[i]]);
swap(s[X[i]], s[Y[i]]);
swap(rs[s[X[i]]], rs[s[Y[i]]]);
/*
cout<<"idx "<<md<<':';
for (int j=0; j<N; j++) cout<<idx[j]<<' ';
cout<<'\n';
cout<<"ridx "<<md<<':';
for (int j=0; j<N; j++) cout<<ridx[j]<<' ';
cout<<'\n';
cout<<"s "<<md<<':';
for (int j=0; j<N; j++) cout<<s[j]<<' ';
cout<<'\n';
cout<<"rs "<<md<<':';
for (int j=0; j<N; j++) cout<<rs[j]<<' ';
cout<<'\n';
*/
while (cur<N&&ridx[cur]==rs[cur]) cur++;
if (cur<N) p[cnt]=ridx[cur], q[cnt]=rs[cur], swap(s[p[cnt]], s[q[cnt]]), swap(rs[s[p[cnt]]], rs[s[q[cnt]]]), cur++, cnt++;
while (cur<N&&ridx[cur]==rs[cur]) cur++;
}
//cout<<"debug "<<l<<' '<<r<<' '<<cur<<' '<<md<<'\n';
if (cur<N) l=md+1;
else
{
r=md;
for (int i=0; i<md; i++) P[i]=p[i], Q[i]=q[i];
}
}
return l;
}
Compilation message
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:10:39: warning: unused parameter 'M' [-Wunused-parameter]
10 | int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
| ~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4620 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
3 |
Incorrect |
1 ms |
4620 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |