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 maxn=1e6+5;
int cnt[maxn],f[maxn];
long long minimum_walk(std::vector<int> p, int s) {
long long ans=0;
int n=(int)p.size();
for(int i=0;i<n;i++){
if(!f[i] && i!=p[i]){
int u=i,mn=i,mx=i;
while(!f[u]){
f[u]=1;
mn=min(mn,u);
mx=max(mx,u);
u=p[u];
}
cnt[mn]++;
cnt[mx]--;
}
ans+=abs(i-p[i]);
}
for(int i=1;i<=n;i++) cnt[i]+=cnt[i-1];
int d=0;
for(int i=n-1;i>=s;i--){
if(cnt[i]) d=1;
else ans+=2*d;
}
d=0;
for(int i=0;i<s;i++){
if(cnt[i]) d=1;
else ans+=2*d;
}
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... |