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;
typedef pair<int, int> pii;
const int maxn = 1000010;
int N, S;
int A[maxn], vis[maxn], Mx[maxn];
vector<pii> V;
ll minimum_walk(std::vector<int> p, int s) {
N = p.size();
S = s;
for(int i = 0; i < N; i++) {
A[i] = p[i];
}
ll ret = 0;
for(int i = 0; i < N; i++) {
if(vis[i]) continue;
int mx = -1e9;
int mn = 1e9;
int u = i;
while(!vis[u]) {
vis[u] = 1;
if(u <= S) {
mx = max(mx, u);
}
if(u >= S) {
mn = min(mn, u);
}
ret += abs(u - A[u]);
u = A[u];
}
V.push_back(pii(S - mx, mn - S));
}
V.push_back(pii(0, 0));
sort(V.begin(), V.end());
for(int i = (int)V.size() - 1; i >= 0; i--) {
Mx[i] = V[i].second;
Mx[i] = max(Mx[i], Mx[i + 1]);
}
int Mn = 1e9;
for(int i = 0; i < V.size(); i++) {
Mn = min(Mn, V[i].first + Mx[i + 1]);
}
ret += 2 * Mn;
return ret;
}
Compilation message (stderr)
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:55:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < V.size(); i++) {
~~^~~~~~~~~~
# | 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... |