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 long long
struct thing {
int u, v, w;
bool operator < (const thing &b) const {
return w < b.w;
}
};
const int maxn = 1e6 + 5, INF = 1e9;
int vis[maxn];
int last[maxn];
int minimum_walk(vector<int32_t> p, int32_t s) {
int n = p.size();
for (int i=0;i<n;i++) vis[i] = false;
int ans = 0;
vector<pair<int,int>> vec;
for (int i=0;i<n;i++) if (!vis[i]) {
if (p[i]==i && i==s) {
vec.push_back({s, s});
}
if (p[i]==i) continue;
int u = i;
pair<int,int> val = {u, u};
while (true) {
vis[u] = 1;
val.first = min(u, val.first), val.second = max(u, val.second);
int v = p[u];
ans += abs(v-u);
if (v==i) break;
u = v;
}
vec.push_back(val);
}
int sz = vec.size();
for (int i=1;i<sz;i++) {
if (vec[i].first > vec[i-1].second) ans += (vec[i].first - vec[i-1].second)*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... |