Submission #1030369

#TimeUsernameProblemLanguageResultExecution timeMemory
1030369shiomusubi496Ancient Books (IOI17_books)C++17
50 / 100
91 ms15184 KiB
#include "books.h" #include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i) #define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i) #define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i) #define all(v) begin(v), end(v) #define rall(v) rbegin(v), rend(v) using namespace std; using ll = long long; template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; } template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; } long long minimum_walk(std::vector<int> p, int s) { int n = p.size(); if (is_sorted(all(p))) return 0; ll ans = 0; { while (p.back() == n - 1) { if (s == n - 1) { --s; ans += 2; } p.pop_back(); --n; } reverse(all(p)); rep (i, n) p[i] = n - 1 - p[i]; s = n - 1 - s; while (p.back() == n - 1) { if (s == n - 1) { --s; ans += 2; } p.pop_back(); --n; } reverse(all(p)); rep (i, n) p[i] = n - 1 - p[i]; s = n - 1 - s; } ll cur = 0; vector<bool> seen(n); rep (i, n) { if (seen[i]) continue; int j = i; ll mx = 0; do { seen[j] = true; chmax(mx, j); j = p[j]; } while (j != i); if (cur < i) ans += 2; chmax(cur, mx); } rep (i, n) ans += abs(p[i] - 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...