답안 #295589

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
295589 2020-09-09T18:54:10 Z williamMBDK 고대 책들 (IOI17_books) C++14
0 / 100
1 ms 512 KB
#include<bits/stdc++.h>
using namespace std;
#include "books.h"
#define int long long
long long minimum_walk(std::vector<signed> p, signed s) {
	int N = p.size();
	vector<int> nodes (N, -1);
	int c = 0;
	int sum = 0;
	for(int i = 0; i < N; i++){
		if(nodes[i] == -1){
			if(p[i] == i) continue;
			int j = i;
			while(j != i){
				sum += abs(j - p[j]); //right?
				nodes[j] = c;
				j = p[j];
			}
		}
		c++;
	}
	priority_queue<pair<int,int>> q;
	vector<vector<pair<int,int>>> adj (c);
	for(int i = 0; i < N; i++){
		while(!q.empty() && q.top().second == nodes[i]){
			q.pop();
		}
		if(!q.empty()){
			auto nb = q.top(); q.pop();
			adj[nb.second].push_back({i - nb.first, nodes[i]});
			adj[nodes[i]].push_back({i - nb.first, nb.second});
		}
		q.push({i,nodes[i]});
	}
	priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
	vector<bool> intree (c);
	intree[0] = 1;
	for(auto e : adj[0]) pq.push(e);
	int tree = 0;
	for(int i = 0; i < N - 1; i++){
		while(intree[pq.top().second]) pq.pop();	
		auto t = pq.top(); pq.pop();
		intree[t.second] = 1;
		tree += t.first;
		for(auto e : adj[t.second]){
			pq.push(e);
		}
	}
	int md = LLONG_MAX;
	for(int i = s; i < N; i++){
		if(nodes[i] != -1){
			md = i - s;
		}
	}
	for(int i = s; i >= 0; i--){
		if(nodes[i] != -1){
			md = min(md, s - i);
		}
	}
	return md + sum + tree;
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 384 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 384 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 384 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 512 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 384 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -