제출 #46900

#제출 시각아이디문제언어결과실행 시간메모리
46900SpaimaCarpatilor고대 책들 (IOI17_books)C++17
50 / 100
244 ms111996 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...