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 inf 1000000000000000000ll
#define all(x) (x).begin(),(x).end()
#define ab(x) ((x)>0?(x):-(x))
#define L long long
using namespace std;
L n;
L chk[1000010];
struct S{
L s,e;
};
vector<S>v;
vector<L>w;
L minimum_walk(vector<int>p,int s){
L i,ret=0,cntgroup=-1,startdist=inf;
n=p.size();
for(i=0;i<n;i++)
if(p[i]!=i)
w.push_back(i);
vector<L>::iterator it=lower_bound(all(w),s+1);
if(it!=w.end())
startdist=min(startdist,*it-s);
it=upper_bound(all(w),s);
if(it!=w.begin())
startdist=min(startdist,(*prev(it))-s);
if(startdist==inf) return 0;
for(i=0;i<n;i++)
{
if(p[i]==i) p[i]=-1;
}
for(i=0;i<n;i++)
{
if(p[i]==-1) continue;
if(!chk[i])
{
L s=i,e=i;
L temp=p[i];
ret+=ab(p[i]-i);
chk[temp]=1;
while(temp!=i)
{
e=max(e,temp);
ret+=ab(p[temp]-temp);
temp=p[temp];
chk[temp]=1;
}
v.push_back((S){s,e});
}
}
sort(all(v),[](S a,S b){
return a.s<b.s;
});
L emax=-1;
for(i=0;i<v.size();i++)
{
//printf("s e %lld %lld\n",v[i].s,v[i].e);
if(v[i].s>emax) cntgroup++;
emax=max(emax,v[i].e);
}
//printf("%lld %lld %lld\n",ret,cntgroup,startdist);
return ret+cntgroup*2+startdist*2;
}
Compilation message (stderr)
books.cpp: In function 'long long int minimum_walk(std::vector<int>, int)':
books.cpp:60:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=0;i<v.size();i++)
~^~~~~~~~~
# | 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... |