Submission #423109

#TimeUsernameProblemLanguageResultExecution timeMemory
423109MDarioAncient Books (IOI17_books)C++11
50 / 100
179 ms13280 KiB
#include "books.h" #include<bits/stdc++.h> using namespace std; typedef long long ll; #define F first #define S second long long minimum_walk(vector<int> p, int s){ ll n=p.size(), r=0, x=n, y=0, z=-1; bool b[n]; for(int i=0; i<n; i++){ b[i]=0; if(p[i]==i)b[i]=1; } vector<pair<ll, ll>> v; for(ll i=0; i<n; i++){ if(!b[i]){ //cout << z << " "; if(i>=z){ if(z!=-1)r+=2*(i-z); x=i; } r+=p[i]-i; y=p[i]; while(y!=i){ z=max(z, y); b[y]=1; r+=abs(p[y]-y); y=p[y]; } b[y]=1; z=max(z, y); v.push_back({x, z}); } } if(v.empty())return 0; unique(v.begin(), v.end()); sort(v.begin(), v.end()); if(s>v.back().S)return r+2*(s-v.back().S); if(s<v[0].F) return r+2*(v[0].F-s); x=0; // for(int i=0; i<v.size(); i++){ // if(i+1==v.size()){ // if(s>=v[i].F&&s<=v[i].S){ // x=min(s-v[i].F, v[i].S-s); // break; // } // } // else if(v[i+1].F!=v[i].F){ // if(s>=v[i].F&&s<=v[i].S){ // x=min(s-v[i].F, v[i].S-s); // break; // } // } // } return r+2*x; }
#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...