Submission #54102

#TimeUsernameProblemLanguageResultExecution timeMemory
54102chpipisAncient Books (IOI17_books)C++11
0 / 100
3 ms648 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> temp, segs; ll ans = 0; for (int i = 0; i < n; i++) { if (visit[i]) continue; int j = i; int mx = j; while (!visit[j]) { mx = max(mx, j); visit[j] = true; ans += abs(j - p[j]); j = p[j]; } temp.pb(mp(i, mx)); } segs.pb(temp[0]); for (auto it : temp) { if (it.fi < segs.back().se) segs.back().se = max(segs.back().se, it.se); else segs.pb(it); } int sz = (int)segs.size(); for (int i = 0; i < sz - 1; i++) { ans += (segs[i + 1].fi - segs[i].se) * 2; } 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...