Submission #1061030

#TimeUsernameProblemLanguageResultExecution timeMemory
1061030onbertAncient Books (IOI17_books)C++17
50 / 100
108 ms43200 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; #define int long long const int maxn = 1e6 + 5, INF = 1e9; int p[maxn]; int vis[maxn]; bool taken[maxn]; vector<pair<int,int>> vec = {{-1, -1}}; int l, r; void expand(int L, int R) { vector<int> add; while (l > L) { l--; if (p[l]!=l) add.push_back(vis[l]); } while (r<R) { r++; if (p[r]!=r) add.push_back(vis[r]); } for (int i:add) expand(vec[i].first, vec[i].second); } int minimum_walk(vector<int32_t> P, int32_t s) { int n = P.size(); for (int i=0;i<n;i++) p[i] = P[i]; for (int i=0;i<n;i++) vis[i] = false; int ans = 0; int cnt = 0; for (int i=0;i<n;i++) if (!vis[i]) { if (p[i]==i && i==s) { cnt++; vis[i] = cnt; vec.push_back({s, s}); } if (p[i]==i) continue; int u = i; pair<int,int> val = {u, u}; cnt++; while (true) { vis[u] = cnt; val.first = min(u, val.first); val.second = max(u, val.second); int v = p[u]; ans += abs(v-u); if (v==i) break; u = v; } vec.push_back(val); } l = s, r = s; expand(vec[vis[s]].first, vec[vis[s]].second); bool done = false; while (l>0 || r<n-1) { for (int dist = 1; ; dist++) { int L = l-dist, R = r + dist; if (L<0 && R>=n) {done = true; break;} if (L>=0 && p[L]!=L) { ans += dist*2; expand(L, r); break; } if (R<n && p[R]!=R) { ans += dist*2; expand(l, R); break; } } if (done) break; } 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...