Submission #961034

#TimeUsernameProblemLanguageResultExecution timeMemory
961034MarwenElarbiAncient Books (IOI17_books)C++17
70 / 100
2061 ms23040 KiB
#include <bits/stdc++.h> //#include "books.h" using namespace std; bool vis[1000005]; vector<int> l(1000000),r(1000000); long long minimum_walk(std::vector<int> p, int s){ int n=p.size(); long long ans=0; int en=s; int bg=s; //cout << ans<<endl; vector<int> cycle; for (int i = 0; i < n; ++i) { ans+=abs(p[i]-i); int cur=i; cycle.clear(); if(p[cur]!=cur){ en=max(en,cur); bg=min(bg,cur); } if(vis[cur]) continue; int mn=1e6+5; int mx=-1; while(vis[cur]==0){ cycle.push_back(cur); vis[cur]=1; mx=max(mx,cur); mn=min(mn,cur); cur=p[cur]; } for(int u:cycle){ //cout <<u<<" "; l[u]=mn; r[u]=mx; } } //cout <<"nabba"<<endl; int curl=s;int ml=l[s]; int curr=s;int mr=r[s]; while(curr<en||curl>bg){ //cout <<curl<<" "<<curr<<endl; if(curr<mr){ curr++; mr=max(mr,r[curr]); ml=min(ml,l[curr]); }else if(curl>ml){ curl--; mr=max(mr,r[curl]); ml=min(ml,l[curl]); }else{ //cout <<"hey"<<endl; bool ok=0; int costl=0; int x=mr; int cl=mr; while(x>bg){ cl=min(cl,l[x]); if(x==cl){ costl+=2; cl--; } x--; if(r[x]>mr){ ok=true; break; } } x=ml; int cr=ml; int costr=0; while(x<en){ cr=max(cr,r[x]); if(x==cr){ cr++; costr+=2; } x++; if(l[x]<ml){ ok=true; break; } } if(ok){ if(costl>costr){ mr=cr; ans+=costr; }else{ ml=cl; ans+=costl; } }else{ ans+=costl+costr; break; } } } 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...