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;
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e6+10;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int mark[MAXN];
pair<ll, int> dfs(vector<int>& p, int ver){
mark[ver]=1;
pair<ll, int> curr={abs(ver-p[ver]), max(ver, p[ver])};
if(!mark[p[ver]]){
pair<ll, int> temp=dfs(p, p[ver]);
curr.fi+=temp.fi;
curr.se=max(curr.se, temp.se);
}
return curr;
}
ll minimum_walk(vector<int> p, int s) {
while(!p.empty() && p.back()==(int)p.size()-1) p.pop_back();
int n=(int)p.size();
ll ans=0;
vector<pii> proc;
for(int i=0;i<n;i++){
if(mark[i]) continue;
pair<ll, int> temp=dfs(p, i);
ans+=temp.fi;
proc.pb({i, temp.se});
}
sort(all(proc));
int last=0;
for(auto u : proc){
ans+=2ll*max(0ll, (ll)u.fi-(ll)last);
last=max(last, u.se);
}
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... |