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 f[maxn],cnt,d[maxn],num;
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;cnt++;
while(!f[u]) f[u]=cnt,u=p[u];
}
ans+=abs(i-p[i]);
}
int mn=n,r=0;
for(int i=0;i<n;i++){
r=max(r,i);
while(r<n && num<cnt){
if(f[r]){
num-=!!d[f[r]];
d[f[r]]++;
num+=!!d[f[r]];
}
r++;
}
if(num==cnt) mn=min(mn,max(r-1,s)-min(i,s));
if(f[i]){
num-=!!d[f[i]];
d[f[i]]--;
num+=!!d[f[i]];
}
}
return ans+2*mn;
}
# | 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... |