Submission #893206

#TimeUsernameProblemLanguageResultExecution timeMemory
893206vjudge1Ancient Books (IOI17_books)C++17
0 / 100
0 ms348 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; using vi = vector<int>; using ii = pair<int, int>; struct UF { vi e; UF(int N) : e(N, -1) {} int repr(int u) { while(e[u] >= 0) u = e[u]; return u; } int join(int u, int v) { u = repr(u); v = repr(v); if(u == v) return 0; if(e[u] >= e[v]) swap(u, v); e[u] += e[v]; e[v] = u; return 1; } }; long long minimum_walk(vi p, int s) { int n = p.size(); vi ip(n); for(int i = 0; i < n; ++i) ip[p[i]] = i; using ll = long long; ll re = 0, nrcic = 0; vi viz(n, 0); for(int i = 0; i < n; ++i) { if(!viz[i]) { int u = i; while(!viz[u]) { viz[u] = 1; u = p[u]; } ++nrcic; } } for(int i = 0; i < n; ++i) { re += abs(p[i] - i); } re += 2 * (nrcic - 1); for(int i = n - 1; i >= 0; --i) { if(p[i] != i) break; else re -= 2; } return re; }
#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...