Submission #153027

#TimeUsernameProblemLanguageResultExecution timeMemory
153027mieszko11bAncient Books (IOI17_books)C++14
22 / 100
168 ms16380 KiB
#include "books.h" #include <bits/stdc++.h> using ll = long long; using namespace std; int n; bool vis[1007]; ll dist[1007]; long long minimum_walk(std::vector<int> p, int s) { ll res = 0; n = p.size(); for(int i = 0 ; i < n ; i++) { if(vis[i]) continue; int w = i; while(!vis[w]) { vis[w] = 1; res += abs(p[w] - w); w = p[w]; } } for(int i = 0 ; i < n ; i++) dist[i] = 1e9; dist[s] = 0; memset(vis, 0, sizeof vis); deque<int> Q; Q.push_front(s); while(!Q.empty()) { int w = Q.front(); Q.pop_front(); vis[w] = 1; int a, b; if(p[w] >= w) a = w + 1, b = p[w]; else a = p[w], b = w - 1; for(int x = a ; x <= b ; x++) if(!vis[x]) dist[x] = min(dist[x], dist[w]), Q.push_front(x); if(w + 1 < n && !vis[w + 1]) { dist[w + 1] = min(dist[w + 1], dist[w] + 1); Q.push_back(w + 1); } if(w - 1 >= 0 && !vis[w - 1]) { dist[w - 1] = min(dist[w - 1], dist[w] + 1); Q.push_back(w - 1); } } ll maxd = 0; for(int i = 0 ; i < n ; i++) if(i != p[i]) maxd = max(maxd, dist[i]); return res + maxd * 2; }
#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...