Submission #1068716

#TimeUsernameProblemLanguageResultExecution timeMemory
1068716BoasFountain Parks (IOI21_parks)C++17
70 / 100
3533 ms90020 KiB
#include "parks.h" #include <bits/stdc++.h> using namespace std; typedef vector<int> vi; typedef vector<signed> vi32; typedef vector<bool> vb; typedef vector<vi> vvi; typedef set<int> si; typedef pair<int, int> ii; typedef tuple<int, int, int> iii; typedef vector<ii> vii; typedef set<iii> siii; typedef set<ii> sii; typedef vector<vii> vvii; #define pb push_back #define loop(x, i) for (int i = 0; i < x; i++) #define ALL(x) begin(x), end(x) #define sz(x) (int)x.size() vii adj = {{-2, 0}, {2, 0}, {0, 2}, {0, -2}}; siii benchPoints; // neighbour count void addNeighbour(int x, int y, int dif = 1) { if (benchPoints.count({1, x, y})) { benchPoints.erase({1, x, y}); if (dif == 1) { benchPoints.insert({2, x, y}); } } else if (benchPoints.count({2, x, y})) { benchPoints.erase({2, x, y}); benchPoints.insert({2 + dif, x, y}); } else if (benchPoints.count({3, x, y})) { benchPoints.erase({3, x, y}); if (dif == 1) throw; benchPoints.insert({2, x, y}); } else if (dif == 1) { benchPoints.insert({1, x, y}); } } int construct_roads(vi xs, vi ys) { map<ii, int> fountains; map<ii, int> roads; // set<ii> gotBench; int n = sz(xs); loop(n, i) { fountains[{xs[i], ys[i]}] = i; } vb vis(n); vi u, v, a, b; a = b = vi(n - 1); int visCnt = 1; auto dfs = [&](auto &&self, int i) -> void { int x = xs[i], y = ys[i]; for (const auto &[dx, dy] : adj) { ii pos = {x + dx, y + dy}; if (fountains.count(pos)) { int j = fountains[pos]; if (vis[j]) continue; if (dx != 0) { roads[{x + dx / 2, y}] = sz(u); addNeighbour(x + dx / 2, y + 1); addNeighbour(x + dx / 2, y - 1); } else { roads[{x, y + dy / 2}] = sz(u); addNeighbour(x + 1, y + dy / 2); addNeighbour(x - 1, y + dy / 2); } u.pb(i); v.pb(j); vis[j] = 1; visCnt++; self(self, j); } } }; vis[0] = 1; dfs(dfs, 0); if (visCnt != n) return 0; for (int benchesBuilt = 0; benchesBuilt < n - 1; benchesBuilt++) { auto [cnt, x, y] = *benchPoints.begin(); benchPoints.erase(benchPoints.begin()); ii loc; if (roads.count({x + 1, y})) { loc = {x + 1, y}; addNeighbour(x + 2, y, -1); } else if (roads.count({x - 1, y})) { loc = {x - 1, y}; addNeighbour(x - 2, y, -1); } else if (roads.count({x, y + 1})) { loc = {x, y + 1}; addNeighbour(x, y + 2, -1); } else { loc = {x, y - 1}; addNeighbour(x, y - 2, -1); } int ix = roads[loc]; roads.erase(loc); a[ix] = x; b[ix] = y; } build(u, v, a, b); return 1; }
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...