Submission #960953

#TimeUsernameProblemLanguageResultExecution timeMemory
960953MarwenElarbiAncient Books (IOI17_books)C++17
50 / 100
102 ms19820 KiB
#include <bits/stdc++.h> #include "books.h" using namespace std; long long minimum_walk(std::vector<int> p, int s){ int n=p.size(); long long ans=0; bool vis[n]; vector<int> cycles(n); memset(vis,0,sizeof vis); for (int i = 0; i < n; ++i) { ans+=abs(p[i]-i); } int en=n-1; for (int i = n-1; i >= 0; --i) { en=i; if(p[i]!=i) break; } //cout << ans<<endl; for (int i = 0; i < n; ++i) { int cur=i; if(vis[cur]) continue; int mn=1e6+5; int mx=-1; while(vis[cur]==0){ vis[cur]=1; mx=max(mx,cur); mn=min(mn,cur); cur=p[cur]; } cycles[mn]++; cycles[mx]--; } int cur=0; for (int i = 0; i < en; ++i) { cur+=cycles[i]; if(cur==0) ans+=2; } 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...