Submission #1048656

#TimeUsernameProblemLanguageResultExecution timeMemory
1048656Gromp15Ancient Books (IOI17_books)C++17
50 / 100
89 ms20076 KiB
#include <bits/stdc++.h> #include "books.h" #define all(x) x.begin(), x.end() using namespace std; template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } long long minimum_walk(std::vector<int> p, int s) { int n = p.size(); vector<bool> vis(n); vector<long long> cost(n); vector<int> to(n); long long ans = 0; for (int i = 0; i < n; i++) if (i != p[i] && !vis[i]) { int right = i; int tmp = i; while (!vis[tmp]) { ckmax(right, tmp); ans += abs(tmp - p[tmp]), vis[tmp] = 1, tmp = p[tmp]; } to[i] = right; } int on = 0; fill(all(vis), 0); for (int i = 0; i < n; i++) if (i != p[i] && !vis[i]) { int tmp = i; while (!vis[tmp]) vis[tmp] = 1, tmp = p[tmp]; if (i <= on) ckmax(on, to[i]); else ans += 2 * (i - on), on = to[i]; } 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...