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;
long long minimum_walk(std::vector<int> p, int s){
int n=p.size();
long long ans=0;
bool vis[n];
vector<int> cycles(n);
memset(vis,0,sizeof vis);
for (int i = 0; i < n; ++i)
{
ans+=abs(p[i]-i);
}
int en=n-1;
for (int i = n-1; i >= 0; --i)
{
en=i;
if(p[i]!=i) break;
}
//cout << ans<<endl;
for (int i = 0; i < n; ++i)
{
int cur=i;
if(vis[cur]) continue;
int mn=1e6+5;
int mx=-1;
while(vis[cur]==0){
vis[cur]=1;
mx=max(mx,cur);
mn=min(mn,cur);
cur=p[cur];
}
cycles[mn]++;
cycles[mx]--;
}
int cur=0;
for (int i = 0; i < en; ++i)
{
cur+=cycles[i];
if(cur==0) 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... |