제출 #216014

#제출 시각아이디문제언어결과실행 시간메모리
216014emil_physmath고대 책들 (IOI17_books)C++17
0 / 100
5 ms512 KiB
#include "books.h"
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

long long minimum_walk(vector<int> a, int s)
{
    int n = a.size();
    long long ans = 0;
    vector<bool> used(n);
    vector<pair<int, int> > p;
    for (int i = 0; i < n; ++i)
        if (!used[i])
        {
            int j = i;
            p.push_back({i, i});
            do {
                used[j] = true;
                j = a[j];
                p.back().second = max(p.back().second, j);
            } while(j != i);
        }
    sort(p.begin(), p.end());
    int r = 0;
    for (pair<int, int>& x: p)
    {
        ans += (x.second - x.first) * 2;
        if (x.first <= r)
            r = max(r, x.second);
        else
        {
            ans += 2 * (x.first - r);
            // ans = max(ans, 2LL * x.first);
            r = x.second;
        }
    }
    /*for (int i = 0; i < n; ++i)
        ans += abs(a[i] - i);*/
    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...