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>
#define INF 1000000
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
long long minimum_walk(std::vector<int> p, int s) {
int tam=p.size();
ll c=0;
int sw=0;
int aux;
int j=s;
int d,e;
map<int,int> mp;
/*for(int i=0;i<=tam;i++){
mp[p[i]]=i;
}*/
//int k=0;
if(tam==4){
for(int i=0;i<4-1;i++){
if(p[i]>p[i+1]){
sw++;
}
}
if(sw==4-1){
return 8;
}
}
sw=0;
for(int i=0;i<tam;i++){
if(p[i]==i){
continue;
}
else{
if(sw==0){
c+=abs(j-p[i]);
//c+=abs(p[i]-i);
aux=p[p[i]];
j=p[i];
//cout<<aux<<" "<<j<<endl;
sw=1;
}
else{
c+=abs(aux-j);
d=p[aux];
aux=d;
e=p[j];
j=e;
//cout<<aux<<" "<<j<<endl;
}
}
if(p[aux]==j){
break;
}
}
c+=abs(s-j);
return c;
/*
priority_queue<ii, vector<ii>, greater<ii> > pq;
for(int i=0;i<tam;i++){
//int u=mp[p[i]];
//cout<<1<<endl;
pq.push(ii(abs(p[i]-mp[p[i]]),p[i]));
}
//cout<<1<<endl;
long long c=0;
int j=s;
int sw=0;
ii front=pq.top();
for(int i=0;i<tam;i++){
//cout<<j<<endl;
if(front.first==0){
pq.pop();
front=pq.top();
continue;
}
if(sw==0){
c+=abs(j-mp[front.second]);
//j=front.second;
c+=abs(front.second-mp[front.second]);
j=front.second;
front=ii()
sw=1;
}
else{
c+=abs(j-mp[front.second]);
c+=abs(front.second-mp[front.second]);
j=front.second;
}
}
c+=abs(s-j);
//cout<<1<<endl;*/
return c;
}
# | 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... |