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 "books.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN=1e6+6;
vector<int> v;
int n, ori, atual, bons;
ll respf, ciclos;
bool melhora() {
if(atual>=n) return 0;
while(bons<n&&v[atual]==atual) atual++;
// printf("atual %d\n", atual);
int mao=v[atual];
v[atual]=-1;
while(mao!=-1) {
respf+=abs(mao-atual);
atual=mao;
swap(mao, v[atual]);
if(v[atual]==atual) bons++;
}
// for(auto cur : v) printf("%d ", cur);
// printf(" >> %lld // %d\n", respf, atual);
return 1;
}
long long minimum_walk(std::vector<int> P, int S) {
n=P.size(); v=P; ori=S; atual=ori;
for(int i=0; i<n; i++) if(v[i]==i) bons++;
while(v[atual]==atual) atual++, respf++;
while(bons<n) melhora(), ciclos++;
respf+=abs(atual-ori);
// printf("%lld // %d\n", respf, ciclos);
respf-=(ciclos-1);
return respf;
}
# | 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... |