#include "sorting.h"
#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define chmax(x, v) x = max(x, v)
#define chmin(x, v) x = min(x, v)
#define all(x) x.begin(), x.end()
#define pb push_back
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N_VALS = 1e6;
int nVals, nTours;
vector<int> vals, tmp;
vector<pii> modifs;
bool vu[N_VALS];
int nCycles(){
for (int ind = 0; ind < nVals; ++ind)
vu[ind] = false;
int nb = 0;
//cout << "BEGIN" << endl;
for (int depart = 0; depart < nVals; ++depart){
if (!vu[depart]){
nb++;
int noeud = depart;
while (!vu[noeud]){
// cout << "NOEUD " << noeud << endl;
vu[noeud] = true;
noeud = vals[noeud];
}
}
}
//cout << "END" << endl;
return nb;
}
void faireSwaps(int borneMax){
for (int ind = 0; ind < nVals; ++ind)
vals[ind] = tmp[ind];
for (int ind = 0; ind < borneMax; ++ind)
swap(vals[modifs[ind].x], vals[modifs[ind].y]);
for (int ind = borneMax - 1; ind >= 0; --ind)
swap(vals[modifs[ind].x], vals[modifs[ind].y]);
}
int findSwapPairs(int N, int S[], int M, int X[], int Y[], int P[], int Q[]) {
//initialisation.
nVals = N, nTours = M;
vals.resize(nVals), tmp.resize(nVals);
for (int ind = 0; ind < nVals; ++ind)
vals[ind] = tmp[ind] = S[ind];
modifs.resize(nTours);
for (int ind = 0; ind < nTours; ++ind)
modifs[ind] = {X[ind], Y[ind]};
//Dichotomie pour trouver R.
int gauche = 1, droite = nTours; //vu que la fin est exclue
while (droite > gauche){
int milieu = (droite + gauche)/2;
faireSwaps(milieu);
// cout << "m " << milieu << " " << gauche << " " << droite << endl;
if (milieu >= nVals - nCycles())
droite = milieu;
else
gauche = milieu + 1;
}
int res = gauche;
faireSwaps(res);
int nb = 0;
for (int ind = 0; ind < nVals; ++ind){
while (vals[ind] != ind){
P[nb] = ind;
Q[nb] = vals[ind];
swap(vals[ind], vals[vals[ind]]);
nb++;
}
}
vector<int> permutation;
permutation.resize(nVals);
for (int ind = 0; ind < nVals; ++ind)
permutation[ind] = ind;
for (int iModif = res - 2; iModif >= 0; --iModif){
swap(permutation[modifs[iModif].x], permutation[modifs[iModif].y]);
swap(vals[modifs[iModif].x], vals[modifs[iModif].y]);
P[iModif] = vals[P[iModif]];
Q[iModif] = vals[Q[iModif]];
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |