# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
424781 | saleh | Ancient Books (IOI17_books) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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() {}