Submission #1080161

#TimeUsernameProblemLanguageResultExecution timeMemory
1080161TB_Ancient Books (IOI17_books)C++17
0 / 100
1 ms348 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define fo(i, n) for(ll i = 0; i<(n); i++)
#define F first
#define S second
#define pb push_back
#define deb(x) cout << #x << " = " << (x) << endl
#define deb2(x, y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl

typedef vector<ll> vl;
typedef vector<vl> vvl;

vl color, nextB;
ll ans = 0, c = 0;

void dfs(int u){
    if(color[u]!=-1) return;
    color[u] = c;
    dfs(nextB[u]);
    ans+=abs(nextB[u]-u);
}

long long minimum_walk(std::vector<int> p, int s){
    ll n = p.size();
    fo(i, n)nextB.pb(p[i]);
    color.assign(n, -1);
    fo(i, n){
        if(nextB[i] == i|| color[i]!=-1) continue;
        dfs(i);
        c++;
    }
    // priority_queue<tuple<ll, ll, ll>> pq;
    // fo(i, n){
    //     fo(j, n){

    //     }
    // }

	return ans+(c-1)*2;
}
#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...