제출 #726857

#제출 시각아이디문제언어결과실행 시간메모리
726857SanguineChameleon즐거운 행로 (APIO20_fun)C++17
100 / 100
292 ms21856 KiB
#include "fun.h"
#include <bits/stdc++.h>
using namespace std;

vector<int> createFunTour(int N, int Q) {
	pair<int, int> best = {N + 1, -1};
	for (int i = 0; i < N; i++) {
		int sz = attractionsBehind(0, i);
		if (sz * 2 >= N) {
			best = min(best, {sz, i});
		}
	}
	int root = best.second;
	vector<int> ch;
	vector<int> depth(N);
	for (int i = 0; i < N; i++) {
		depth[i] = hoursRequired(root, i);
		if (depth[i] == 1) {
			ch.push_back(i);
		}
	}
	bool merge = (int)(ch.size()) <= 2;
	ch.resize(3, -1);
	vector<pair<int, vector<pair<int, int>>>> sub(3);
	for (int i = 0; i < 3; i++) {
		sub[i].first = i;
	}
	for (int i = 0; i < N; i++) {
		if (i == root) {
			continue;
		}
		int id = 2;
		for (int j = 0; j < 2; j++) {
			if (hoursRequired(ch[j], i) + 1 == depth[i]) {
				id = j;
				break;
			}
		}
		sub[id].second.push_back({depth[i], i});
	}
	for (int i = 0; i < 3; i++) {
		sort(sub[i].second.begin(), sub[i].second.end());
	}
	int prv = -1;
	vector<int> res;
	vector<int> depths;
	for (int i = 0; i < N - 1; i++) {
		if (sub[0].second.size() > sub[1].second.size()) {
			swap(sub[0], sub[1]);
		}
		if (sub[1].second.size() > sub[2].second.size()) {
			swap(sub[1], sub[2]);
		}
		if (sub[0].second.size() > sub[2].second.size()) {
			swap(sub[0], sub[2]);
		}
		pair<pair<int, int>, int> best = {{-1, -1}, -1};
		if (!merge && sub[0].second.size() + sub[1].second.size() <= sub[2].second.size()) {
			int prv_depth = (depths.empty() ? N + 1 : depths.back());
			if (prv == sub[0].first) {
				if (prv_depth < sub[1].second.back().first) {
					best = {sub[1].second.back(), 1};
				}
				prv = 0;
			}
			else if (prv == sub[1].first) {
				if (prv_depth < sub[0].second.back().first) {
					best = {sub[0].second.back(), 0};
				}
				prv = 0;
			}
			else {
				prv = 1;
			}
			sub[0].first = 0;
			sub[1].first = 0;
			sub[2].first = 1;
			merge = true;
		}
		for (int k = 0; k < 2; k++) {
			if (best.second != -1) {
				break;
			}
			for (int j = 0; j < 3; j++) {
				if (k == 0 && prv == sub[j].first) {
					continue;
				}
				if (!sub[j].second.empty()) {
					best = max(best, make_pair(sub[j].second.back(), j));
				}
			}
		}
		depths.push_back(sub[best.second].second.back().first);
		res.push_back(sub[best.second].second.back().second);
		sub[best.second].second.pop_back();
		prv = sub[best.second].first;
	}
	res.push_back(root);
	return res;
}
#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...