This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int c=200002;
int lo, hi, mid, st=0, a[c], ia[c], b[c], ib[c], cs1, cs2;
vector<int> e, f;
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
lo=-1, hi=n;
//lo=1, hi=3;
while(hi-lo>1) {
mid=(hi+lo)/2, st=0;
for (int i=0; i<n; i++) a[i]=s[i], b[i]=i, ia[s[i]]=i, ib[i]=i;
e.clear(), f.clear();
for (int i=mid-1; i>=0; i--) swap(ib[b[x[i]]], ib[b[y[i]]]), swap(b[x[i]], b[y[i]]);
/*if (mid==1) {
for (int i=0; i<n; i++) cout << a[i] << " ";
cout << "\n";
for (int i=0; i<n; i++) cout << b[i] << " ";
cout << "\n";
}*/
cs1=x[0], cs2=y[0];
if (mid) {
swap(ia[a[cs1]], ia[a[cs2]]), swap(a[cs1], a[cs2]);
swap(ib[b[cs1]], ib[b[cs2]]), swap(b[cs1], b[cs2]);
}
for (int i=0; i<n; i++) {
if (ia[i]!=ib[i]) {
cs1=ia[i], cs2=ib[i];
/*if (mid==1) {
cout << "csere " << cs1 << " " << cs2 << "\n";
}*/
e.push_back(cs1), f.push_back(cs2);
//p[st]=cs1, q[st]=cs2;
swap(ia[a[cs1]], ia[a[cs2]]), swap(a[cs1], a[cs2]);
st++;
if (st<mid) {
cs1=x[st], cs2=y[st];
swap(ia[a[cs1]], ia[a[cs2]]), swap(a[cs1], a[cs2]);
swap(ib[b[cs1]], ib[b[cs2]]), swap(b[cs1], b[cs2]);
}
/*if (mid==1 && st==0) {
for (int i=0; i<n; i++) cout << ia[i] << " ";
cout << "\n";
for (int i=0; i<n; i++) cout << ib[i] << " ";
cout << "\n";
}*/
}
}
//cout << mid << " " << st << endl;
if (st<=mid) {
hi=mid;
for (int i=0; i<mid; i++) p[i]=e[i], q[i]=f[i];
}
else lo=mid;
}
//for(int i=0; i<hi; i++) cout << p[i] << " " << q[i] << "\n";
return hi;
}
Compilation message (stderr)
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:7:39: warning: unused parameter 'm' [-Wunused-parameter]
7 | int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
| ~~~~^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |