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 "books.h"
using namespace std;
using ll=long long;
const ll MAX_TAILLE=1000*1000+5;
ll nbVal,rep,posDeb,nbCompo,valFin,nbBon;
vector<ll> obj,listeCompo;
ll idCompo[MAX_TAILLE],finCompo[MAX_TAILLE],tailleCompo[MAX_TAILLE];
void DFS(ll pos) {
if (idCompo[pos]==0) {
idCompo[pos]=nbCompo;
finCompo[pos]=valFin;
tailleCompo[nbCompo]++;
DFS(obj[pos]);
}
}
ll minimum_walk(vector<int> p, int s) {
posDeb=s;
nbVal=p.size();
for (auto i:p) {
obj.push_back(i);
}
for (ll i=nbVal-1;i>=0;i--) {
if (idCompo[i]==0) {
nbCompo++;
valFin=i;
DFS(i);
if (tailleCompo[nbCompo]==1) {
nbBon++;
}
}
rep+=abs(i-obj[i]);
}
int pos=0;
while (pos!=nbVal-1 and nbBon<nbVal) {
if (finCompo[pos]==pos) {
pos++;
rep+=2;
}
else {
nbBon+=tailleCompo[idCompo[pos]];
pos=finCompo[pos];
}
}
return rep;
}
# | 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... |