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;
const int INF=1e9+7;
long long minimum_walk(vector<int> p, int s) {
int n=p.size();
long long ans=0;
vector<bool> vis(n, false);
vector<pair<int,int> > ranges;
for(int i=0; i<n; i++){
if(vis[i]) continue;
int v=i;
int cnt=0;
int l=i;
int r=i;
while(!vis[v]){
vis[v]=true;
cnt++;
l=min(l, v);
r=max(r, v);
ans += abs(p[v]-v);
v=p[v];
}
if(cnt!=1){
ranges.push_back({l, r});
}
}
ranges.push_back({0, 0});
sort(ranges.begin(), ranges.end());
int last=0;
for(auto [l, r]: ranges){
if(last < l){
ans += 2*(l-last);
}
last=max(last, r);
}
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... |