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;
bool vis[1005];
long long minimum_walk(vector<int> p, int s) {
vector<pair<int,int>>v;
int n=p.size();
long long ans=0;
for(int i=0;i<n;i++){
ans+=abs(i-p[i]);
v.push_back({min(i,p[i]),max(i,p[i])});
}
sort(v.begin(),v.end());
int last=0;
for(auto i:v){
for(int j=max(last+1,i.first+1);j<=i.second;j++){
vis[j]=true;
last=max(last,i.second);
}
}
int k=-1;
for(int i=n-1;i>=0;i--){
if(p[i]!=i){
k=i;
break;
}
}
k++;
for(int i=1;i<k;i++){
if(!vis[i]){
ans+=2;
}
}
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... |