Submission #468140

#TimeUsernameProblemLanguageResultExecution timeMemory
468140nonsensenonsense1Fun Tour (APIO20_fun)C++17
100 / 100
284 ms21816 KiB
#include "fun.h" #include <vector> #include <algorithm> const int N = 100000; int size[N], dist[N]; std::vector<int> root, ans; std::vector<std::pair<int, int> > sub[3]; int max_size() { return std::max(sub[0].size(), std::max(sub[1].size(), sub[2].size())); } int get_far(int cur) { int len = 0, from = -1; for (int i = 0; i < (int)root.size(); ++i) if (i != cur && !sub[i].empty() && sub[i].back().first > len) { len = sub[i].back().first; from = i; } return from; } void swap(int i, int j, int &cur) { if (cur == i) cur = j; else if (cur == j) cur = i; std::swap(sub[i], sub[j]); } void update_ans(int ind) { ans.push_back(sub[ind].back().second); sub[ind].pop_back(); } std::vector<int> createFunTour(int n, int q) { int ind = 0; for (int i = 0; i < n; ++i) { size[i] = attractionsBehind(0, i); if (size[i] * 2 > n && size[i] < size[ind]) ind = i; } for (int i = 0; i < n; ++i) { dist[i] = hoursRequired(ind, i); if (dist[i] == 1) root.push_back(i); } for (int i = 0; i < n; ++i) if (i != ind) { std::pair<int, int> cur(dist[i], i); if (hoursRequired(i, root[0]) == dist[i] - 1) sub[0].push_back(cur); else if (hoursRequired(i, root[1]) == dist[i] - 1) sub[1].push_back(cur); else sub[2].push_back(cur); } for (int i = 0; i < 3; ++i) std::sort(sub[i].begin(), sub[i].end()); int cur = -1; while (max_size() * 2 < n - 1) { cur = get_far(cur); update_ans(cur); --n; } int far_from = get_far(-1); if (cur != -1 && far_from != cur) { cur = far_from; update_ans(cur); } if (root.size() == 3) { if (sub[0].size() < sub[1].size()) swap(0, 1, cur); if (sub[1].size() < sub[2].size()) swap(1, 2, cur); if (sub[0].size() < sub[1].size()) swap(0, 1, cur); root.pop_back(); } if (cur == 2) cur = 1; sub[1].insert(sub[1].end(), sub[2].begin(), sub[2].end()); std::sort(sub[1].begin(), sub[1].end()); while ((cur = get_far(cur)) != -1) update_ans(cur); ans.push_back(ind); if ((cur = get_far(cur)) != -1) update_ans(cur); 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...