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;
int vis[1000001],l[1000001];
vector <pair <int, pair <int, int>>> e;
vector <int> ve[1000001];
int f(int i){
return (l[i]==i?i:l[i]=f(l[i]));
}
int unite(int i, int j){
i=f(i);
j=f(j);
if (i==j)
return 0;
l[j]=i;
return 1;
}
long long minimum_walk(vector <int> p, int s){
int x=0,n=p.size();
iota(l,l+n,0);
long long d=0;
for (int i=0;i<n;i++){
if (!vis[i]){
int x=i;
while (true){
vis[x]=1;
ve[i].push_back(x);
unite(x,i);
d+=abs(x-p[x]);
x=p[x];
if (x==i)
break;
}
}
}
vector <pair <int, int>> tmp;
for (int i=0;i<n;i++){
if (ve[i].empty()||(ve[i].size()==1&&i!=s))
continue;
sort(ve[i].begin(),ve[i].end());
tmp.push_back({ve[i][0],ve[i].back()});
}
sort(tmp.begin(),tmp.end());
int cur=0;
for (auto [l,r]:tmp){
x+=max(l-cur,0);
cur=max(cur,r);
}
return x*2+d;
}
# | 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... |