Submission #826918

#TimeUsernameProblemLanguageResultExecution timeMemory
826918vnm06Ancient Books (IOI17_books)C++14
0 / 100
1 ms340 KiB
#include<bits/stdc++.h> #include "books.h" using namespace std; int n; bool used[1000005]; int a[1000005]; long long ans=0; int bri=0, Le, Ri; pair<int, int> pr[1000005]; void dfs(int i) { while(1) { used[i]=1; long long st=a[i]-i; if(st<0) st=-st; ans+=st; if(a[i]==Le) break; ///cout<<i<<" "<<ans<<endl; i=a[i]; if(i>Ri) Ri=i; } } long long minimum_walk(std::vector<int> p, int s) { n=p.size(); for(int i=0; i<n; i++) a[i]=p[i]; for(int i=0; i<n; i++) { if(!used[i]) { Le=Ri=i; dfs(i); pr[bri]={Le, Ri}; bri++; } } ///cout<<ans<<endl; sort(pr, pr+bri); Le=pr[0].first; Ri=pr[0].second; for(int i=1; i<bri; i++) { if(pr[i].first>Ri) { ans+=2; Le=pr[i].first; Ri=pr[i].second; } else if(pr[i].second<=Ri) Ri=pr[i].second; } 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...