제출 #641452

#제출 시각아이디문제언어결과실행 시간메모리
641452ggohAncient Books (IOI17_books)C++14
0 / 100
1 ms212 KiB
#include "books.h" #include<bits/stdc++.h> using namespace std; #define sz(v) ((int)(v).size()) typedef long long lint; int b,n,sz,cnt; int v[1000002],rev[1000002]; vector<int>go; lint ans=0; void dfs(int p,int q) { if(b==-1) { ans+=p; return ; } int r=p; if(q!=b) { r=rev[b]; ans+=abs(r-p); if(q!=-1)rev[q]=r; } ans+=abs(b-r); int tmp=b; q=go[b]; go[b]=b;rev[b]=b; while(b-1>=0 && go[b-1]==b-1)b--; b--; dfs(tmp,q); } lint minimum_walk(vector<int> p, int s) { n=sz(p); go=p; for(int i=0;i<n;i++)rev[go[i]]=i; b=n; while(b-1>=0 && go[b-1]==b-1)b--; b--; if(b==-1)return 0; int t=-1,maxi=-1; for(int i=rev[b]-1;i>=0;i--) { if(go[i]!=i) { if(maxi<go[i]) { maxi=go[i]; t=i; } } } if(t==-1)t=b; ans+=t; maxi=go[t]; go[t]=-1; dfs(t,maxi); 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...