#include<bits/stdc++.h>
#include "sorting.h"
using namespace std;
const int MAX_SWAP=1000*1000;
int nbVal,nbSwap,posModif,nouvVal,deb,fin,mid,verif;
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);
}
/*posModif=0;
for (int j=0;j<nbVal;j++) {
if (val[j]!=obj[j]) {
posModif=j;
}
}*/
nouvVal=posDansObj[val[posModif]];
nouv.push_back({posModif,nouvVal});
swap(val[posModif],val[nouvVal]);
maj(posModif);
maj(nouvVal);
}
verif=1;
for (int i=0;i<nbVal;i++) {
if (val[i]!=i) {
verif=0;
}
}
if (verif==0) {
deb=mid+1;
}
if (verif==1) {
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
408 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
408 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
4 ms |
724 KB |
Output is correct |
22 |
Correct |
5 ms |
724 KB |
Output is correct |
23 |
Correct |
4 ms |
724 KB |
Output is correct |
24 |
Correct |
6 ms |
724 KB |
Output is correct |
25 |
Correct |
3 ms |
724 KB |
Output is correct |
26 |
Correct |
4 ms |
724 KB |
Output is correct |
27 |
Correct |
4 ms |
820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
596 KB |
Output is correct |
2 |
Correct |
9 ms |
576 KB |
Output is correct |
3 |
Correct |
11 ms |
616 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
5 ms |
596 KB |
Output is correct |
7 |
Correct |
8 ms |
656 KB |
Output is correct |
8 |
Correct |
14 ms |
648 KB |
Output is correct |
9 |
Correct |
12 ms |
652 KB |
Output is correct |
10 |
Correct |
13 ms |
628 KB |
Output is correct |
11 |
Correct |
12 ms |
632 KB |
Output is correct |
12 |
Correct |
9 ms |
612 KB |
Output is correct |
13 |
Correct |
12 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
596 KB |
Output is correct |
2 |
Correct |
9 ms |
576 KB |
Output is correct |
3 |
Correct |
11 ms |
616 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
4 ms |
596 KB |
Output is correct |
6 |
Correct |
5 ms |
596 KB |
Output is correct |
7 |
Correct |
8 ms |
656 KB |
Output is correct |
8 |
Correct |
14 ms |
648 KB |
Output is correct |
9 |
Correct |
12 ms |
652 KB |
Output is correct |
10 |
Correct |
13 ms |
628 KB |
Output is correct |
11 |
Correct |
12 ms |
632 KB |
Output is correct |
12 |
Correct |
9 ms |
612 KB |
Output is correct |
13 |
Correct |
12 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Execution timed out |
1074 ms |
28088 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |