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>
#include "sorting.h"
using namespace std;
const int MAX_SWAP=600*1000+5;
int nbVal,nbSwap,posModif,nouvVal,deb,fin,mid;
int autreSwap[MAX_SWAP][2];
int val[MAX_SWAP];
int posDansObj[MAX_SWAP];
int obj[MAX_SWAP];
vector<pair<int,int>> nouv;
vector<pair<int,int>> rep;
set<int> listePb;
set<int>::iterator it;
void maj(int pos) {
listePb.erase(pos);
if (val[pos]!=obj[pos]) {
listePb.insert(pos);
}
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
nbVal=N;
nbSwap=M;
for (int i=0;i<nbVal;i++) {
val[i]=S[i];
}
for (int i=0;i<nbSwap;i++) {
autreSwap[i][0]=X[i];
autreSwap[i][1]=Y[i];
}
fin=nbSwap;
rep.resize(nbSwap);
while (deb!=fin) {
mid=(deb+fin)/2;
nouv.clear();
listePb.clear();
for (int i=0;i<nbVal;i++) {
obj[i]=i;
posDansObj[i]=i;
}
for (int i=0;i<nbVal;i++) {
val[i]=S[i];
}
for (int i=mid-1;i>=0;i--) {
posDansObj[obj[autreSwap[i][0]]]=autreSwap[i][1];
posDansObj[obj[autreSwap[i][1]]]=autreSwap[i][0];
swap(obj[autreSwap[i][0]],obj[autreSwap[i][1]]);
}
for (int i=0;i<nbVal;i++) {
maj(i);
}
for (int i=0;i<mid;i++) {
posDansObj[obj[autreSwap[i][0]]]=autreSwap[i][1];
posDansObj[obj[autreSwap[i][1]]]=autreSwap[i][0];
swap(obj[autreSwap[i][0]],obj[autreSwap[i][1]]);
swap(val[autreSwap[i][0]],val[autreSwap[i][1]]);
maj(autreSwap[i][0]);
maj(autreSwap[i][1]);
if (listePb.empty()) {
posModif=0;
}
else {
it=listePb.begin();
posModif=(*it);
}
nouvVal=posDansObj[val[posModif]];
nouv.push_back({posModif,nouvVal});
swap(val[posModif],val[nouvVal]);
maj(posModif);
maj(nouvVal);
}
if (!listePb.empty()) {
deb=mid+1;
}
else {
fin=mid;
if (nouv.size()<rep.size()) {
rep=nouv;
}
}
}
for (int i=0;i<deb;i++) {
P[i]=rep[i].first;
Q[i]=rep[i].second;
}
return deb;
}
# | 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... |