제출 #982829

#제출 시각아이디문제언어결과실행 시간메모리
982829Lalic고대 책들 (IOI17_books)C++17
50 / 100
130 ms43480 KiB
#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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...