Submission #935241

#TimeUsernameProblemLanguageResultExecution timeMemory
935241MinaRagy06Fun Tour (APIO20_fun)C++17
100 / 100
174 ms21528 KiB
#include <bits/stdc++.h> #ifdef MINA #include "grader.cpp" #endif #include "fun.h" using namespace std; #define ll long long #define sz(x) (int) x.size() int n; vector<int> ans; const int MAXN = 100'005; vector<int> gud[3]; int dist[MAXN], comp[MAXN]; int c, lst = -1; void solve() { if (sz(gud[1]) < sz(gud[0])) { swap(gud[0], gud[1]); if (lst != -1) lst = !lst; } if (sz(gud[1]) > sz(gud[0])) lst = 0; // for (auto i : gud[0]) { // cout << dist[i] << ' '; // } // cout << '\n'; // for (auto i : gud[1]) { // cout << dist[i] << ' '; // } // cout << '\n'; // for (auto i : ans) { // cout << i << ' '; // } // cout << '\n'; while (sz(gud[0]) || sz(gud[1])) { array<int, 3> mx = {-1, -1, -1}; for (int k = 0; k < 2; k++) { if (lst == k || gud[k].empty()) continue; mx = max(mx, {dist[gud[k].back()], k, gud[k].back()}); } lst = mx[1]; ans.push_back(mx[2]); gud[lst].pop_back(); } ans.push_back(c); // for (auto i : ans) { // cout << i << ' '; // } // cout << '\n'; } vector<int> createFunTour(int _n, int q) { n = _n; array<int, 2> mn = {(int)1e9, -1}; for (int i = 0; i < n; i++) { int sz = attractionsBehind(0, i); if (sz >= (n + 1) / 2) { mn = min(mn, {sz, i}); } } c = mn[1]; vector<int> v; for (int i = 0; i < n; i++) { dist[i] = hoursRequired(c, i); if (dist[i] == 1) v.push_back(i); } for (int i = 0; i < n; i++) { if (i == c) continue; if (hoursRequired(i, v[0]) + 1 == dist[i]) { comp[i] = 0; gud[0].push_back(i); } else if (hoursRequired(i, v[1]) + 1 == dist[i]) { comp[i] = 1; gud[1].push_back(i); } else { comp[i] = 2; gud[2].push_back(i); } } if (gud[2].empty()) { for (int k = 0; k < 3; k++) { sort(gud[k].begin(), gud[k].end(), [&] (int x, int y) {return dist[x] < dist[y];}); } solve(); return ans; } for (int k = 0; k < 3; k++) { sort(gud[k].begin(), gud[k].end(), [&] (int x, int y) {return dist[x] < dist[y];}); } int mxm; while ((mxm = max({sz(gud[0]), sz(gud[1]), sz(gud[2])})) < sz(gud[0]) + sz(gud[1]) + sz(gud[2]) - mxm) { array<int, 3> mx = {-1, -1, -1}; for (int k = 0; k < 3; k++) { if (lst == k || gud[k].empty()) continue; mx = max(mx, {dist[gud[k].back()], gud[k].back(), k}); } lst = mx[2]; ans.push_back(mx[1]); gud[lst].pop_back(); } if (sz(gud[0]) + sz(gud[1]) + sz(gud[2]) == 0) return ans; int LST = lst; for (int k = 0; k < 3; k++) { if (sz(gud[k]) == mxm) { vector<int> v2; for (int j = 0; j < 3; j++) if (j != k) v2.push_back(j); if (lst == v2.back()) lst = v2[0]; for (auto j : gud[v2.back()]) gud[v2[0]].push_back(j); gud[v2.back()].clear(); break; } } for (int k = 0; k < 3; k++) { sort(gud[k].begin(), gud[k].end(), [&] (int x, int y) {return dist[x] < dist[y];}); } // cout << lst << '\n'; // for (auto i : gud[0]) { // cout << dist[i] << ' '; // } // cout << '\n'; // for (auto i : gud[1]) { // cout << dist[i] << ' '; // } // cout << '\n'; // for (auto i : gud[2]) { // cout << dist[i] << ' '; // } // cout << '\n'; if (lst != -1) { for (int i = sz(gud[lst]) - 1; i >= 0; i--) { if (dist[gud[lst][i]] != dist[gud[lst].back()]) break; if (comp[gud[lst][i]] != LST) { swap(gud[lst][i], gud[lst][sz(gud[lst]) - 1]); lst = -1; break; } } } // cout << ">>> "; // for (auto i : ans) cout << i << ' '; // cout << '\n'; if (gud[0].empty()) { swap(gud[0], gud[2]); if (lst == 2) lst = 0; } if (gud[1].empty()) { swap(gud[1], gud[2]); if (lst == 2) lst = 1; } solve(); return ans; }

Compilation message (stderr)

fun.cpp: In function 'std::vector<int> createFunTour(int, int)':
fun.cpp:89:14: warning: operation on 'mxm' may be undefined [-Wsequence-point]
   89 |  while ((mxm = max({sz(gud[0]), sz(gud[1]), sz(gud[2])})) < sz(gud[0]) + sz(gud[1]) + sz(gud[2]) - mxm) {
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fun.cpp:89:14: warning: operation on 'mxm' may be undefined [-Wsequence-point]
#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...