제출 #718568

#제출 시각아이디문제언어결과실행 시간메모리
718568Jarif_Rahman고대 책들 (IOI17_books)C++17
0 / 100
1 ms212 KiB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

ll minimum_walk(vector<int> p, int s){
    int n = p.size();
    ll ans = 0;

    vector<int> cycle_id(n, -1);
    int k = 0;
    for(int i = 0; i < n; i++){
        if(cycle_id[i] != -1) continue;
        int x = p[i];
        cycle_id[i] = k;
        ans+=abs(i-x);
        while(x != i){
            cycle_id[x] = k;
            ans+=abs(p[x]-x);
            x = p[x];
        }
        k++;
    }

    vector<int> cnt(k, 0);
    for(int i = 0; i < n; i++) cnt[cycle_id[i]]++;
    vector<bool> nested(k, 0);
    set<int> st;
    for(int i = 0; i < n; i++){
        st.insert(cycle_id[i]);
        if(st.size() > 2) nested[cycle_id[i]] = 1;
        cnt[cycle_id[i]]--;
        if(cnt[cycle_id[i]] == 0) st.erase(cycle_id[i]);
    }

    ans+=k-1;
    for(int i = 1; i < k; i++) if(!nested[i]) ans++;
    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...