Submission #992404

#TimeUsernameProblemLanguageResultExecution timeMemory
992404bachhoangxuanAncient Books (IOI17_books)C++17
0 / 100
1 ms348 KiB
#include "books.h" #include<bits/stdc++.h> using namespace std; const int maxn=1e6+5; int f[maxn],cnt,d[maxn],num; long long minimum_walk(std::vector<int> p, int s) { long long ans=0; int n=(int)p.size(); for(int i=0;i<n;i++){ if(!f[i] && i!=p[i]){ int u=i;cnt++; while(!f[u]) f[u]=cnt,u=p[u]; } ans+=abs(i-p[i]); } int mn=n,r=0; for(int i=0;i<n;i++){ r=max(r,i); while(r<n && num<cnt){ if(f[r]){ num-=!!d[f[r]]; d[f[r]]++; num+=!!d[f[r]]; } r++; } if(num==cnt) mn=min(mn,max(r-1,s)-min(i,s)); if(f[i]){ num-=!!d[f[i]]; d[f[i]]--; num+=!!d[f[i]]; } } return ans+2*mn; }
#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...