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;
typedef long long ll;
typedef pair<int,int> pii;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mk make_pair
#define pb push_back
#define fr first
#define sc second
ll minimum_walk(vector<int> p, int s) {
int n = sz(p);
vector<bool> vis(n);
vector<pii> aux, range;
ll ans = 0;
for(int i = 0; i < n; i++) {
int x = i, m = i;
while(!vis[x]) {
vis[x] = 1;
x = p[x];
m = max(m, x);
}
ans += abs(p[i]-i);
if(i != m) aux.pb({i,m});
}
for(int i = 0; i < sz(aux);) {
int l = aux[i].fr, r = aux[i].sc, j = i;
while(j < sz(aux) and aux[j].fr < r) {
r = max(r, aux[j].sc);
j++;
}
i=j;
range.pb({l,r});
}
for(int i = 0; i < sz(range); i++) {
if(i == 0) ans += 2*range[i].fr;
else ans += 2*(range[i].fr - range[i-1].sc);
}
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... |