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 x first
#define y second
using namespace std;
const int INF = 1e9;
typedef long long ll;
typedef pair <int,int> pi;
typedef vector <int> vec;
using namespace std;
vec a;
int n;
int c[1000005],mn,mx;
vector <pi> line;
void dfs(int x) {
if(c[x]) return;
c[x] = 1;
mn = min(mn,x);
mx = max(mx,x);
dfs(a[x]);
}
ll minimum_walk(vec P,int st) {
ll ans = 0;
a = P;
n = a.size();
line.push_back({0,0});
for(int i = 0;i < n;i++) {
ans += abs(i-a[i]);
if(c[i]||i == a[i]) continue;
mn = INF, mx = -INF;
dfs(i);
if(mn > line.back().y) {
line.push_back({mn,mx});
}
else {
line.back().y = max(line.back().y,mx);
}
}
int m = line.size();
for(int i = 1;i < m;i++) {
ans += (line[i].x-line[i-1].y)*2;
}
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... |