Submission #877400

#TimeUsernameProblemLanguageResultExecution timeMemory
877400MilosMilutinovicAncient Books (IOI17_books)C++14
12 / 100
0 ms600 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; long long minimum_walk(vector<int> p, int s) { int n = (int) p.size(); vector<int> all; long long cost = 0; vector<bool> was(n); vector<int> R(n); for (int i = 0; i < n; i++) { if (was[i]) { continue; } int x = i; vector<int> vec; while (!was[x]) { was[x] = true; vec.push_back(x); cost += abs(p[x] - x); x = p[x]; } sort(vec.begin(), vec.end()); if (vec.size() > 1) { all.push_back(vec[0]); R[vec[0]] = vec.back(); } } int pos = 0; int to = -1; for (int i = 0; i < n; i++) { if (p[i] == i) { continue; } if (to < i) { pos = i; } to = max(to, R[i]); } cost += 2 * pos; return cost; }
#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...