Submission #54098

#TimeUsernameProblemLanguageResultExecution timeMemory
54098chpipisAncient Books (IOI17_books)C++11
0 / 100
3 ms840 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; #define fi first #define se second #define mp make_pair #define pb push_back typedef long long ll; typedef pair<int, int> ii; const int MAX_N = 1e6 + 5; bitset<MAX_N> visit; vector<int> p, from; int n; ll minimum_walk(vector<int> _p, int s) { assert(s == 0); p = _p; n = (int)p.size(); from.resize(n); for (int i = 0; i < n; i++) from[p[i]] = i; visit.reset(); vector<ii> segs; vector<ll> val; for (int i = 0; i < n; i++) { if (visit[i]) continue; int j = i; int mx = j; val.pb(0); while (!visit[j]) { mx = max(mx, j); visit[j] = true; val.back() += abs(j - p[j]); j = p[j]; } segs.pb(mp(i, mx)); } ll ans = 0; for (auto it : val) ans += it; int sz = (int)segs.size(); for (int i = 0, j; i < sz; i = j) { j = i; int mx = segs[i].se; while (j < sz && segs[j].fi <= mx) { mx = max(mx, segs[j].se); j++; } ans += mx + 1 - from[segs[i].fi]; } 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...