# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
424781 | 2021-06-12T10:05:39 Z | saleh | Ancient Books (IOI17_books) | C++17 | 0 ms | 0 KB |
#include "books.h" #include <bits/stdc++.h> using namespace std; const int MAXN = 1000 * 1000 + 23; int mark[MAXN], cnt = 0, ans, jav; long long minimum_walk(std::vector<int> p, int s) { if (s != 0) return 0; for (int i = 0; i < n; i++) if (p[i] != i && mark[i] == 0) { int tmp = i, pre = i; mark[i] = ++cnt; while ((tmp = p[tmp]) != i) { mark[tmp] = cnt; jav += abs(pre - tmp); pre = tmp; } ans = max(ans, i); } return jav + ans * 2; } //int main() {}