#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;
deque<int> listePb;
void maj(int pos) {
if (val[pos]!=obj[pos]) {
listePb.push_back(pos);
}
while (!listePb.empty() and val[listePb.front()]==obj[listePb.front()]) {
listePb.pop_front();
}
}
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 {
posModif=listePb.front();
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
388 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 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 |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
388 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
25 |
Correct |
2 ms |
724 KB |
Output is correct |
26 |
Correct |
1 ms |
724 KB |
Output is correct |
27 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
233 ms |
22728 KB |
Output is correct |
16 |
Correct |
265 ms |
31024 KB |
Output is correct |
17 |
Correct |
279 ms |
32652 KB |
Output is correct |
18 |
Correct |
92 ms |
30412 KB |
Output is correct |
19 |
Correct |
212 ms |
32068 KB |
Output is correct |
20 |
Correct |
212 ms |
32944 KB |
Output is correct |
21 |
Correct |
208 ms |
33024 KB |
Output is correct |
22 |
Correct |
242 ms |
28228 KB |
Output is correct |
23 |
Correct |
266 ms |
34264 KB |
Output is correct |
24 |
Correct |
297 ms |
33504 KB |
Output is correct |
25 |
Correct |
293 ms |
33000 KB |
Output is correct |
26 |
Correct |
203 ms |
31684 KB |
Output is correct |
27 |
Correct |
181 ms |
30500 KB |
Output is correct |
28 |
Correct |
284 ms |
33252 KB |
Output is correct |
29 |
Correct |
274 ms |
32856 KB |
Output is correct |
30 |
Correct |
165 ms |
30560 KB |
Output is correct |
31 |
Correct |
280 ms |
33520 KB |
Output is correct |
32 |
Correct |
268 ms |
32908 KB |
Output is correct |
33 |
Correct |
288 ms |
33472 KB |
Output is correct |
34 |
Correct |
268 ms |
33480 KB |
Output is correct |
35 |
Correct |
213 ms |
31588 KB |
Output is correct |
36 |
Correct |
74 ms |
30144 KB |
Output is correct |
37 |
Correct |
282 ms |
34432 KB |
Output is correct |
38 |
Correct |
283 ms |
32964 KB |
Output is correct |
39 |
Correct |
321 ms |
33212 KB |
Output is correct |