# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1030400 | vjudge1 | Ancient Books (IOI17_books) | C++17 | 98 ms | 23516 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 "books.h"
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rng(i,l,r) for(int i=(l); i<(r); i++)
#define all(x) x.begin(),x.end()
using ll=long long;
long long minimum_walk(std::vector<int> p, int s) {
int n=p.size();
vector<int> vis(n,0);
ll ans=0;
vector<pair<int,int>> seg;
rep(i,n){
if(vis[i])continue;
if(p[i]==i&&i!=s)continue;
int lf=i;
int ri=i;
int pos=i;
while(!vis[p[pos]]){
ans+=abs(pos-p[pos]);
pos=p[pos];
vis[pos]=1;
lf=min(lf,pos);
ri=max(ri,pos);
}
seg.emplace_back(lf,ri);
}
sort(all(seg));
vector<pair<int,int>> re={seg[0]};
rng(i,1,seg.size()){
if(seg[i].first<=re.back().second){
re.back().second=max(re.back().second,seg[i].second);
}
else{
re.emplace_back(seg[i]);
}
}
rep(i,re.size()-1){
ans+=2*(re[i+1].first-re[i].second);
}
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... |