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;
typedef long long ll;
const int N = 1000123;
bool vis[N];
int n;
long long minimum_walk(std::vector<int> p, int s) {
long long ans = 0;
n = (int)p.size();
for (int i = 0; i < n; i++) {
vis[i] = false;
}
if (s != 0 && p[s] != s) {
return 3304;
}
for (int i = 0; i < n; i++) {
ans += abs (p[i] - i);
}
int mx = 0;
int mm = 0;
for (int i = 0; i < n; i++) {
if (!vis[i]) {
if (p[i] == i)
continue;
if (i > mm) {
mx += i - mm;
}
mm = max (mm, i);
int j = i;
while (p[j] != i) {
j = p[j];
mm = max(mm, j);
vis[j] = true;
}
}
}
return ans + 2 * mx;
}
# | 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... |