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;
const int N=1e6;
int vis[N], n;
long long minimum_walk(vector<int> p, int s) {
while (p.size() && p.back()==(int)p.size()-1) p.pop_back();
n=p.size();
vector<pair<int, int>> v;
long long ans=0;
for (int i=0; i<n; ++i) if (!vis[i]){
int mx=i, mn=i;
while (!vis[i]){
mx=max(mx, i);
mn=min(mn, i);
vis[i]=1;
ans+=abs(p[i]-i);
i=p[i];
}
v.emplace_back(mn, mx);
}
sort(v.begin(), v.end());
int cur=0;
for (auto &i:v){
if (cur<=i.first) ans+=(i.first-cur)*2;
cur=max(cur, 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... |