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;
#define int int64_t
vector<int> p;
int s;
int n;
int res;
vector<int> cycles;
pair<int, int> getCycle(int v, int ind) {
if (cycles[v] != -1) return {-1, -1};
pair<int, int> range = {1e9, -1e9};
while (cycles[v] == -1) {
cycles[v] = ind;
range.first = min(range.first, v);
range.second = max(range.second, v);
res += abs(p[v]-v);
v = p[v];
}
return range;
}
long long minimum_walk(vector<signed> _p, signed _s) {
s = _s;
n = _p.size();
p.resize(n); for (int i = 0; i<n; i++) p[i] = _p[i];
cycles.assign(n, -1);
res = 0;
vector<pair<int, int>> ranges;
for (int i = 0; i<n; i++) {
ranges.push_back(getCycle(i, i));
if (ranges.back().first == ranges.back().second) ranges.pop_back();
}
ranges.push_back({s, s});
sort(ranges.begin(), ranges.end());
set<int> active = {ranges[0].second};
for (int i = 1; i<(int)ranges.size(); i++) {
res += 2*max((int)0, ranges[i].first-*active.rbegin());
active.insert(ranges[i].second);
}
return res;
}
# | 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... |