Submission #832496

#TimeUsernameProblemLanguageResultExecution timeMemory
832496Rafi22Ancient Books (IOI17_books)C++14
100 / 100
132 ms27988 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define pb push_back #define st first #define nd second #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() int inf=1000000007; ll infl=1000000000000000007; const int N=1000007; bool odw[N]; int id[N]; vector<pair<int,int>>V; ll minimum_walk(vector<int>p,int s) { int n=sz(p); ll ans=0; for(int i=0;i<n;i++) ans+=abs(i-p[i]); int c=0; int A=0,B=n-1; while(A<s&&p[A]==A) A++; while(s<B&&p[B]==B) B--; for(int i=0;i<n;i++) { if(odw[i]) continue; int l=inf,r=-inf,x=i; vector<int>X; while(!odw[x]) { odw[x]=1; X.pb(x); l=min(l,x); r=max(r,x); x=p[x]; } if(l==r&&(l<A||l>B)) continue; V.pb({l,r}); for(auto x:X) id[x]=c; c++; } int i=s,j=s; int l=V[id[s]].st,r=V[id[s]].nd; while(l>A||r<B) { while(true) { if(i>l) { i--; l=min(l,V[id[i]].st); r=max(r,V[id[i]].nd); } else if(j<r) { j++; l=min(l,V[id[j]].st); r=max(r,V[id[j]].nd); } else break; } //cout<<l<<" "<<r<<endl; if(l==A&&r==B) break; int cL=0,L=-1,p=inf; for(int k=l;k>=A;k--) { p=min(p,V[id[k]].st); if(V[id[k]].nd>r) { L=k; break; } if(k!=A&&p==k) cL++; } p=-inf; int cR=0,R=-1; for(int k=r;k<=B;k++) { p=max(p,V[id[k]].nd); if(V[id[k]].st<l) { R=k; break; } if(k!=B&&p==k) cR++; } if(L==-1) { ans+=2*(cL+cR); break; } l=L; r=R; ans+=2*min(cL,cR); } return ans; } /* int main() { int nn,s; cin>>nn>>s; vector<int>V(nn); for(int i=0;i<nn;i++) cin>>V[i]; //random_shuffle(all(V)); //for(int i=0;i<nn;i++) cout<<V[i]<<" "; //cout<<endl; cout<<minimum_walk(V,s); return 0; }*/
#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...