Submission #898568

#TimeUsernameProblemLanguageResultExecution timeMemory
898568OAleksaFun Tour (APIO20_fun)C++14
47 / 100
104 ms15168 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; auto Resi2 = [&](vector<int> s1, vector<int> s2) { if (s1.size() > s2.size()) swap(s1, s2); int lst = 0; while (!s2.empty()) { if (lst == 0) { ans.push_back(s2.back()); s2.pop_back(); lst = 1; } else { ans.push_back(s1.back()); s1.pop_back(); lst = 0; } } if (!s1.empty()) { assert(s1.size() == 1); //jednaki su ans.push_back(s1.back()); } }; if (m == 2) { Resi2(ch[0], ch[1]); } else { int lst = -1; 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 && mx > 0) 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; } assert(lst != bst); 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]; }); Resi2(all, ch[bst]); } 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...