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>
long long minimum_walk(std::vector<int> p, int s) {
int len=p.size();
long long ans=0;
int max=len-1;
int pos[len];
for(int i=0;i<len;i++)
{
pos[p[i]]=i;
}
while(pos[max]==max)
{
max=max-1;
if(max==0)
{
return ans;
}
}
int i=0;
int carrying=p[0];
//cout<<"aa"<<endl;
while(p[i]!=-1)
{
if(pos[max]==max)
{
max--;
continue;
}
ans=ans+abs(pos[max]-i);
p[pos[max]]=carrying;
pos[carrying]=pos[max];
if(i==0)
{
p[i]=-1;
}
else p[i]=carrying;
i=pos[max];
ans=ans+abs(max-i);
carrying=p[max];
i=max;
pos[i]=max;
max--;
//cout<<i<<" "<<p[i]<<endl;
}
return ans;
}
# | 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... |