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>
#define int long long
using namespace std;
const int N=1e6+5;
int a[N],par[N],sz[N],n;
bool vis[N];
int getpar(int x){
if (x==par[x]) return x;
return par[x]=getpar(par[x]);
}
void link(int x,int y){
x=getpar(x);
y=getpar(y);
if (x==y) return;
if (sz[x]>sz[y]) swap(x,y);
sz[y]+=sz[x];
par[x]=y;
}
long long minimum_walk(std::vector<int32_t> p, int32_t s) {
n=p.size();int ans=0;
for (int i=1;i<=n;i++) a[i]=p[i-1]+1,par[i]=i,sz[i]=1;
for (int i=1;i<=n;i++) link(a[i],i),ans+=abs(a[i]-i);
int x=0;
for (int i=1;i<=n;i++){
if (vis[getpar(i)] || sz[getpar(i)]==1) continue;
x=i*2-2;
vis[getpar(i)]=1;
}
return x+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... |