Submission #647487

#TimeUsernameProblemLanguageResultExecution timeMemory
647487a_aguiloAncient Books (IOI17_books)C++14
0 / 100
1 ms296 KiB
#include "books.h" #include<bits/stdc++.h> using namespace std; int n; vector<int> father; vector<long long> dist; int root(int node){ if(father[node] == node) return node; return father[node] = root(father[node]); } void mergeComponents(int actNode, int destiny){ int rAct = root(actNode); int rNext = root(destiny); father[rAct] = rNext; if(rAct != rNext) dist[rNext] += (long long)dist[rAct]; dist[rNext] += (long long)abs(destiny - actNode); } bool already(vector<int> p){ for(int i = 0; i < n; ++i){ if(p[i] != i) return false; } return true; } long long minimum_walk(vector<int> p, int s) { n = p.size(); if(already(p)) return 0; father = vector<int>(n); for(int i = 0; i < n; ++i) father[i] = i; dist = vector<long long int>(n, 0); for(int i = 0; i < n; ++i){ mergeComponents(i, p[i]); } if(root(s) == s and dist[s] == 0){ if(s > 0){ mergeComponents(s, s-1); dist[root(s)]++; } else{ mergeComponents(s, s+1); dist[root(s)]++; } } for(int i = 0; i < n-1; ++i){ int rAct = root(i); if(rAct == i and dist[i] == 0) continue; int rNext = root(i+1); if(rAct == rNext) continue; mergeComponents(i, i+1); dist[root(rAct)]++; } return dist[root(s)]; }
#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...