제출 #1351857

#제출 시각아이디문제언어결과실행 시간메모리
1351857Andrey고대 책들 (IOI17_books)C++17
50 / 100
88 ms19940 KiB
#include "books.h"
#include<bits/stdc++.h>
using namespace std;

long long minimum_walk(std::vector<int> p, int s) {
    long long ans = 0;
    while(!p.empty() && p[p.size()-1] == p.size()-1) {
        p.pop_back();
    }
    if(p.empty()) {
        return 0;
    }
    int n = p.size();
    for(int i = 0; i < n; i++) {
        ans+=abs(p[i]-i);
    }
    vector<int> col(n,-1);
    vector<int> br(n);
    for(int i = 0; i < n; i++) {
        int y = i;
        while(col[y] == -1) {
            col[y] = i;
            y = p[y];
            br[i]++;
        }
    }
    vector<int> banana(n);
    int x = 0;
    for(int i = 0; i < n; i++) {
        int c = col[i];
        if(banana[c] == 0) {
            x++;
        }
        banana[c]++;
        if(banana[c] == br[c]) {
            x--;
        }
        if(x == 0 && i < n-1) {
            ans+=2;
        }
    }
    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...