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>
#include "books.h"
using namespace std;
int n;
bool used[1000005];
int a[1000005];
long long ans=0;
int bri=0, Le, Ri;
pair<int, int> pr[1000005];
void dfs(int i)
{
while(1)
{
used[i]=1;
long long st=a[i]-i;
if(st<0) st=-st;
ans+=st;
if(a[i]==Le) break;
i=a[i];
if(i>Ri) Ri=i;
}
}
int tbr=0;
int rm[1000005][2];
long long minimum_walk(std::vector<int> p, int s)
{
n=p.size();
for(int i=0; i<n; i++) a[i]=p[i];
for(int i=0; i<n; i++)
{
if(!used[i])
{
Le=Ri=i;
dfs(i);
pr[bri]={Le, Ri};
bri++;
}
}
///cout<<ans<<endl;
sort(pr, pr+bri);
rm[0][0]=pr[0].first;
rm[0][1]=pr[0].second;
tbr=1;
for(int i=1; i<bri; i++)
{
if(pr[i].first>rm[tbr-1][1])
{
rm[tbr][0]=pr[i].first;
rm[tbr][1]=pr[i].second;
tbr++;
}
else if(pr[i].second>=rm[tbr-1][1]) rm[tbr-1][1]=pr[i].second;
else if(s<=pr[i].second && s>=pr[i].first) ans+=2;
}
ans+=2*(tbr-1);
for(int i=0; i<tbr; i++)
{
if(s<=rm[i][1]) break;
if(rm[i][0]!=rm[i][1]) break;
ans-=2;
}
for(int i=tbr-1; i>=0; i--)
{
if(s>=rm[i][0]) break;
if(rm[i][0]!=rm[i][1]) break;
ans-=2;
}
return ans;
}
# | 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... |