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;
///cout<<i<<" "<<ans<<endl;
i=a[i];
if(i>Ri) Ri=i;
}
}
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);
Le=pr[0].first;
Ri=pr[0].second;
for(int i=1; i<bri; i++)
{
if(pr[i].first>Ri)
{
ans+=2;
Le=pr[i].first;
Ri=pr[i].second;
}
else if(pr[i].second<=Ri) Ri=pr[i].second;
}
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... |