제출 #408175

#제출 시각아이디문제언어결과실행 시간메모리
408175Kevin_Zhang_TW고대 책들 (IOI17_books)C++17
50 / 100
217 ms19716 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; #define pb emplace_back #define AI(i) begin(i), end(i) template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); } template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); } #ifdef KEV #define DE(args...) kout("[ " + string(#args) + " ] = ", args) void kout() { cerr << endl; } template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); } template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; } #else #define DE(...) 0 #define debug(...) 0 #endif const int MAX_N = 300010; #include "books.h" long long minimum_walk(std::vector<int> p, int s) { int n = p.size(); vector<int> cntl(n+1), cntr(n+1); for (int i = 0;i < n;++i) { if (p[i] < i) ++cntl[p[i]+1], --cntl[i+1]; if (p[i] > i) ++cntr[i], --cntr[p[i]]; } if (is_sorted(AI(p))) return 0; ll res = 0; for (int i = 1;i <= n;++i) { cntr[i] += cntr[i-1]; cntl[i] += cntl[i-1]; } int l = 0, r = n-1; while (p[l] == l) ++l; while (p[r] == r) --r; chmax(r, s); chmin(l, s); for (int i = l;i < r;++i) { res += max({cntl[i+1], cntr[i], 1}) * 2; } // if (s > r) res += (s - r) * 2; if (s < l) res += (l - s) * 2; int L= l , R = r; if (s < r && s > l) { int mn = n + n; auto valid = [&](int g) { int l = g, r = g, ml = min(g, p[g]), mr = max(g, p[g]); while (ml < l || mr > r) { while (ml < l) { --l; chmin(ml, p[l]); chmax(mr, p[l]); } while (mr > r) { ++r; chmin(ml, p[r]); chmax(mr, p[r]); } } return ml == L && mr == R; }; for (int i = l;i <= r;++i) { if (valid(i)) chmin(mn, abs(i-s) * 2); } res += mn; } return res; }
#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...