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;
void acsere() {swap(ia[a[cs1]], ia[a[cs2]]), swap(a[cs1], a[cs2]);}
void bcsere() {swap(ib[b[cs1]], ib[b[cs2]]), swap(b[cs1], b[cs2]);}
int findSwapPairs(int n, int s[], int m, int x[], int y[], int p[], int q[]) {
lo=-1, hi=n;
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]]);
cs1=x[0], cs2=y[0];
if (mid) acsere(), bcsere();
for (int i=0; i<n; i++) {
if (ia[i]!=ib[i]) {
cs1=ia[i], cs2=ib[i];
e.push_back(cs1), f.push_back(cs2);
acsere();
st++;
if (st<mid) {
cs1=x[st], cs2=y[st];
acsere(), bcsere();
}
}
}
while(st<mid) e.push_back(0), f.push_back(0), st++;
if (st<=mid) {
hi=mid;
for (int i=0; i<mid; i++) p[i]=e[i], q[i]=f[i];
}
else lo=mid;
}
return hi;
}
Compilation message (stderr)
sorting.cpp: In function 'int findSwapPairs(int, int*, int, int*, int*, int*, int*)':
sorting.cpp:9:39: warning: unused parameter 'm' [-Wunused-parameter]
9 | 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... |