Submission #898542

#TimeUsernameProblemLanguageResultExecution timeMemory
898542OAleksaFun Tour (APIO20_fun)C++14
31 / 100
87 ms15924 KiB
#include "fun.h" #include <bits/stdc++.h> #define f first #define s second using namespace std; vector<int> createFunTour(int N, int Q) { int n = N; int cen = 0; if (n == 2) return {0, 1}; for (int i = 1;i < n;i++) { if (attractionsBehind(cen, i) > n / 2) cen = i; } vector<int> dis(n); for (int i = 0;i < n;i++) { if (i == cen) continue; dis[i] = hoursRequired(cen, i); } vector<int> p; for (int i = 0;i < n;i++) { if (dis[i] == 1) { p.push_back(i); } } int m = p.size(); vector<int> ch[m]; for (int i = 0;i < n;i++) { if (i == cen) continue; for (int j = 0;j < m;j++) { if (j == m - 1) { ch[j].push_back(i); } else { int x = hoursRequired(p[j], i); if (x + 1 == dis[i]) { ch[j].push_back(i); break; } } } } for (int i = 0;i < m;i++) { sort(ch[i].begin(), ch[i].end(), [&](int x, int y) { return dis[x] < dis[y]; }); } vector<int> ans; int lst = 0; if (m == 2) { if (ch[0].size() > ch[1].size()) swap(ch[0], ch[1]); while (!ch[1].empty()) { if (lst == 0) { ans.push_back(ch[1].back()); ch[1].pop_back(); lst = 1; } else { ans.push_back(ch[0].back()); ch[0].pop_back(); lst = 0; } } if (!ch[0].empty()) { //jednake velicine assert(ch[0].size() == 1); ans.push_back(ch[0].back()); } } else { while (1) { int u = 0, mx = 0; for (int i = 0;i < m;i++) { mx = max(mx, (int)ch[i].size()); u += ch[i].size(); } if (mx == u - mx) break; mx = 0; int node = -1; for (int j = 0;j < m;j++) { if (j == lst) continue; if (dis[ch[j].back()] > mx) { mx = dis[ch[j].back()]; node = j; } } assert(node != -1); ans.push_back(ch[node].back()); ch[node].pop_back(); lst = node; } int bst = 0; for (int i = 1;i < m;i++) { if (ch[i].size() > ch[bst].size()) bst = i; } vector<int> all; for (int i = 0;i < m;i++) { if (i == bst) continue; for (auto u : ch[i]) all.push_back(u); } sort(all.begin(), all.end(), [&](int i, int j) { return dis[i] < dis[j]; }); if (ch[bst].back() > all.back()) lst = -1; while (!all.empty() || !ch[bst].empty()) { if (lst != bst) { ans.push_back(ch[bst].back()); ch[bst].pop_back(); lst = bst; } else { ans.push_back(all.back()); all.pop_back(); lst = -1; } } } ans.push_back(cen); return ans; } /* 7 400000 0 1 0 5 0 6 1 2 1 4 2 3 */
#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...