Submission #139123

#TimeUsernameProblemLanguageResultExecution timeMemory
139123zoooma13Ancient Books (IOI17_books)C++14
0 / 100
2 ms376 KiB
#include "bits/stdc++.h" #include "books.h" using namespace std; int n; long long minimum_walk(std::vector<int> p, int s) { n = p.size(); vector <int> sz(n) ,vis(n) ,rep(n); for(int i=0; i<n; i++) if(!vis[i]){ int tot = 0; for(int j=i; !vis[j]; j=p[j]){ tot += abs(p[j]-j); rep[j] = i; vis[j] = 1; } sz[i] = tot; } fill(vis.begin() ,vis.end() ,0); long long ans = 0LL ,lst = 0; for(int i=0; i<n; i++){ if(!vis[rep[i]] && sz[rep[i]]){ ans += sz[rep[i]]+(i-lst); vis[rep[i]] = 1; lst = rep[i]; } } return ans+lst; }
#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...