Submission #593647

#TimeUsernameProblemLanguageResultExecution timeMemory
593647yutabiAncient Books (IOI17_books)C++14
0 / 100
1 ms468 KiB
#include "books.h" #include <bits/stdc++.h> using namespace std; #define pb push_back typedef long long ll; long long minimum_walk(std::vector<int> p, int s) { int n=p.size(); ll ans=0; int mex_low=0; int mex_high=n-1; vector <bool> v(1000007,0); int left_limit=0; int right_limit=n-1; for(int i=0;i<=s;i++) { ans+=abs(i-p[i]); v[p[i]]=1; while(v[mex_low]==1) { mex_low++; } if(mex_low>i) { if(i!=s) ans+=2; left_limit=i+1; } } v=vector <bool> (1000007,0); for(int i=n-1;i>=s;i--) { ans+=abs(i-p[i]); v[p[i]]=1; while(mex_high>=0 && v[mex_high]==1) { mex_high--; } if(mex_high<i) { if(i!=s) ans+=2; right_limit=i-1; } } ans-=abs(s-p[s]); int lptr=s; int rptr=s; v=vector <bool> (1000007,0); mex_low=s; mex_high=s; int mini=p[s]; int maxi=p[s]; v[p[s]]=1; while(lptr>=left_limit && rptr<=right_limit) { while(mini<lptr || rptr<maxi) { while(mini<lptr) { lptr--; v[p[lptr]]=1; mini=min(mini,p[lptr]); maxi=max(maxi,p[lptr]); } while(rptr<maxi) { rptr++; v[p[rptr]]=1; mini=min(mini,p[rptr]); maxi=max(maxi,p[rptr]); } } while(mex_low>=0 && v[mex_low]==1) { mex_low--; } while(v[mex_high]==1) { mex_high++; } if(lptr==left_limit && rptr==right_limit) { break; } ans+=2; mini--; maxi++; } /*if(left_limit>right_limit) { 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...