# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
561026 | neki | Ancient Books (IOI17_books) | C++14 | 208 ms | 48660 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define vc vector
#define all(a) a.begin(), a.end()
#define fi first
#define se second
using namespace std;
ll minimum_walk(vc<int> p, int s){
ll n=p.size();
ll ans=0;
vc<ll> doseg(n);iota(all(doseg), 0);
vc<ll> vis(n, 0);
for(ll i=0;i<n;++i) if(!vis[i]){
vc<ll> cyc;
for(ll cur=i;1;){
vis[cur]=1;
cyc.push_back(cur);
ll nex=p[cur];
ans+=abs(nex-cur);
if(nex==i)break;
cur=nex;
}
ll zac=(*min_element(all(cyc))), konc=(*max_element(all(cyc)));
doseg[konc]=min(doseg[konc], zac);
}
vc<pair<ll, ll>> ints;
for(ll i=0;i<n;++i){
while(ints.size() and doseg[i]<=ints.back().second) ints.pop_back();
if(ints.size()==0) ints.emplace_back((ll)0, i);
else ints.emplace_back(ints.back().second+1, i);
}
ll st;for(ll i=0;i<ints.size();++i) if(ints[i].fi<=s and s<=ints[i].se) st=i;
ll fl=st;for(ll i=st-1;i>=0;--i) if(ints[i].fi!=ints[i].se) fl=i;
ll fr=st;for(ll i=st+1;i<ints.size();++i) if(ints[i].fi!=ints[i].se) fr=i;
ans+=2 * (fr-fl);
return ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |