Submission #932656

#TimeUsernameProblemLanguageResultExecution timeMemory
932656Programmer123Fountain Parks (IOI21_parks)C++17
55 / 100
3074 ms85328 KiB
#include <bits/stdc++.h> #include "parks.h" std::unordered_map<int, int> map[200001]; int N; std::vector<std::pair<int, int>> vld[200000]; std::pair<int, int> assigned[200000]; std::set<std::pair<int, int>> taken; time_t stop; bool backtrack(int n) { if (n == N - 1) return true; if (clock() > stop) return false; for (auto a: vld[n]) { if (taken.count(a)) continue; taken.insert(a); if (backtrack(n + 1)) { assigned[n] = a; return true; } taken.erase(a); } return false; } int construct_roads(std::vector<int> x, std::vector<int> y) { stop = clock() + 3 * CLOCKS_PER_SEC; N = (int) x.size(); if (N == 1) { build({}, {}, {}, {}); return 1; } int maxx = x[0]; int minx = x[0]; int maxy = y[0]; int miny = y[0]; for (int i = 0; i < N; ++i) { maxx = std::max(maxx, x[i]); minx = std::min(minx, x[i]); maxy = std::max(maxy, y[i]); miny = std::min(miny, y[i]); } for (int i = 0; i < N; ++i) { map[x[i]][y[i]] = i; } bool shuffle = false; std::default_random_engine Rand(std::random_device{}()); while (true) { std::vector<int> u, v, a, b; bool seen[N]; for (int i = 0; i < N; ++i) { seen[i] = false; } std::queue<int> bfs; int start = 0; if (shuffle) start = Rand() % N; bfs.push(start); seen[start] = true; auto add = [&](int x, int y, std::vector<int> &data) { if (x < 0 || x > 200000) return; if (map[x].count(y)) data.push_back(map[x][y]); }; auto edges = [&](int x, int y) { std::vector<int> result; add(x, y + 2, result); add(x, y - 2, result); add(x + 2, y, result); add(x - 2, y, result); if (shuffle) { std::shuffle(result.begin(), result.end(), Rand); } return result; }; int rem = N - 1; while (!bfs.empty()) { auto next = bfs.front(); bfs.pop(); for (auto n: edges(x[next], y[next])) { if (!seen[n]) { seen[n] = true; bfs.push(n); rem--; u.push_back(next); v.push_back(n); } } } if (rem) return 0; std::function<std::vector<std::pair<int, int>>(int, int)> valid = [&x, &y, minx, maxx, miny, maxy](int u, int v) -> std::vector<std::pair<int, int>> { if (x[u] == x[v]) { int _y = (y[u] + y[v]) / 2; if (x[u] == minx) return {{minx - 1, _y}}; if (x[u] == maxx) return {{maxx + 1, _y}}; return {{x[u] + 1, _y}, {x[u] - 1, _y}}; } else { int _x = (x[u] + x[v]) / 2; if (y[u] == miny) return {{_x, miny - 1}}; if (y[u] == maxy) return {{_x, maxy + 1}}; return {{_x, y[u] + 1}, {_x, y[u] - 1}}; } }; for (int i = 0; i < N - 1; ++i) { vld[i] = valid(u[i], v[i]); } if (backtrack(0)) { for (int i = 0; i < N - 1; ++i) { a.push_back(assigned[i].first); b.push_back(assigned[i].second); } build(u, v, a, b); return 1; } if (clock() > stop) break; shuffle = true; } return 0; }
#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...