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 nr, N, S, p[1000009], mars[1000009];
bool useful[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;
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 (i != p[i])
{
int l = i, r = p[i];
if (l > r) swap (l, r);
mars[l] ++, mars[r] --;
useful[i] = 1;
}
useful[S] = 1;
int usefulToRight = 0, usefulToLeft = 0;
for (int i=1; i<=N; i++)
usefulToRight += useful[i];
for (int i=1; i<N; i++)
mars[i] += mars[i - 1];
for (int i=1; i<N; i++)
{
usefulToRight -= useful[i];
usefulToLeft += useful[i];
if (usefulToLeft > 0 && usefulToRight > 0 && mars[i] == 0)
ans += 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... |