Submission #992482

#TimeUsernameProblemLanguageResultExecution timeMemory
992482bachhoangxuanAncient Books (IOI17_books)C++17
50 / 100
102 ms22872 KiB
#include "books.h" #include<bits/stdc++.h> using namespace std; const int maxn=1e6+5; int cnt[maxn],f[maxn]; 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,mn=i,mx=i; while(!f[u]){ f[u]=1; mn=min(mn,u); mx=max(mx,u); u=p[u]; } cnt[mn]++; cnt[mx]--; } ans+=abs(i-p[i]); } for(int i=1;i<=n;i++) cnt[i]+=cnt[i-1]; int d=0; for(int i=n-1;i>=0;i--){ if(cnt[i]) d=1; else ans+=2*d; } 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...