제출 #726851

#제출 시각아이디문제언어결과실행 시간메모리
726851SanguineChameleon즐거운 행로 (APIO20_fun)C++17
100 / 100
253 ms22184 KiB
#include "fun.h" #include <bits/stdc++.h> using namespace std; pair<int, vector<int>> solve(int N, vector<pair<int, vector<pair<int, int>>>> sub, int root, bool merge_later) { int prv = -1; vector<int> res; int merge_time = -1; 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}; bool will_merge = false; if (merge_time == -1 && sub[0].second.size() + sub[1].second.size() <= sub[2].second.size()) { will_merge = true; merge_time = i; } if (will_merge && !merge_later) { if (prv == sub[0].first || prv == sub[1].first) { prv = 0; } else if (prv == sub[1].first) { prv = 0; } else { prv = 1; } sub[0].first = 0; sub[1].first = 0; sub[2].first = 1; } 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)); } } } res.push_back(sub[best.second].second.back().second); sub[best.second].second.pop_back(); prv = sub[best.second].first; if (will_merge && merge_later) { if (prv == sub[0].first || prv == sub[1].first) { prv = 0; } else if (prv == sub[1].first) { prv = 0; } else { prv = 1; } sub[0].first = 0; sub[1].first = 0; sub[2].first = 1; } } res.push_back(root); return {merge_time, res}; } 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()); } auto p = solve(N, sub, root, false); int pos = p.first; vector<int> res = p.second; if (pos == -1) { return res; } int prv = N + 1; for (int i = max(0, pos - 5); i < min(N - 1, pos + 5); i++) { int cur = hoursRequired(res[i], res[i + 1]); if (prv < cur) { return solve(N, sub, root, true).second; } prv = cur; } return res; }

컴파일 시 표준 에러 (stderr) 메시지

fun.cpp: In function 'std::vector<int> createFunTour(int, int)':
fun.cpp:91:7: warning: unused variable 'merge' [-Wunused-variable]
   91 |  bool merge = (int)(ch.size()) <= 2;
      |       ^~~~~
#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...