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;
int N, S, p[1000009], nxt[1000009];
bool ap[1000009];
long long minimum_walk (vector < int > pp, int ss)
{
N = pp.size (), S = ss + 1;
for (int i=1; i<=N; i++)
p[i] = pp[i - 1] + 1;
for (int i=1; i<=N; i++)
if (ap[i] == 0)
{
int j = p[i], maxRight = i;
ap[i] = 1;
while (j != i)
ap[j] = 1, maxRight = max (maxRight, j), j = p[j];
nxt[i] = maxRight, j = p[i];
while (j != i)
nxt[j] = maxRight, j = p[j];
}
long long ans = 0;
for (int i=1; i<=N; i++)
{
int curr = i - p[i];
if (curr < 0) curr = -curr;
ans += curr;
}
for (int i=1; i<N; i++)
if (nxt[i] > nxt[i + 1])
nxt[i + 1] = nxt[i];
int target = 0;
for (int i=1; i<=N; i++)
if (p[i] != i)
target = i;
int pos = 1;
while (pos < target)
{
if (pos == nxt[pos]) ans += 2, pos ++;
else pos = nxt[pos];
}
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... |