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 <bits/stdc++.h>
#include "books.h"
using namespace std;
#define int long long
vector< int > DSU;
vector< int > Size;
int getParents(int a){
if(DSU[a] == a)return a;
else return DSU[a] = getParents(DSU[a]);
}
void Union(int a, int b){
a = getParents(a);
b = getParents(b);
if(Size[a] < Size[b]){
DSU[a] = b;
Size[b] += Size[a];
}else{
DSU[b] = a;
Size[a] += Size[b];
}
}
#undef int
long long minimum_walk(vector<int> order, int S) {
#define int long long
int n = order.size();
DSU.resize(n+1);
Size.assign(n+1, 1);
for(int i = 0; i <= n; i++){
DSU[i] = i;
}
int ans = 0;
for(int i = 0; i < n; i++){
ans += abs(order[i]-i);
Union(i, order[i]);
}
int last = -1;
for(int i = S+1; i < n; i++){
if(getParents(i) != getParents(i-1)){
if(last == -1) last = i-1;
if(Size[getParents(i)] >= 2){
ans += 2*(i - last);
last = -1;
}
}
}
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... |