Submission #1060859

#TimeUsernameProblemLanguageResultExecution timeMemory
1060859onbertAncient Books (IOI17_books)C++17
0 / 100
0 ms348 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; #define int long long const int maxn = 1e6 + 5, INF = 1e9; int vis[maxn]; int minimum_walk(vector<int32_t> p, int32_t s) { int n = p.size(); for (int i=0;i<n;i++) vis[i] = false; int ans = 0; vector<pair<int,int>> vec; for (int i=0;i<n;i++) if (!vis[i]) { if (p[i]==i && i==s) { vec.push_back({s, s}); } if (p[i]==i) continue; int u = i; pair<int,int> val = {u, u}; while (true) { vis[u] = 1; 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); } int sz = vec.size(); sort(vec.begin(), vec.end()); if (sz==0) return ans; int mx = vec[0].first; for (int i=1;i<sz;i++) { if (vec[i].first > mx) ans += (vec[i].first - mx)*2; mx = max(vec[i].second, mx); } 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...