Submission #152815

#TimeUsernameProblemLanguageResultExecution timeMemory
152815mieszko11bAncient Books (IOI17_books)C++14
0 / 100
2 ms376 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[0] = 0; memset(vis, 0, sizeof vis); deque<int> Q; Q.push_front(0); while(!Q.empty()) { int w = Q.front(); Q.pop_front(); vis[w] = 1; if(p[w] > w) { for(int x = w + 1 ; x <= p[w] ; 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); } } return res + 2 * dist[n - 1]; }
#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...