diff --git a/cpp/prevoi19_robots.cpp b/cpp/prevoi19_robots.cpp index c26ad4d..0be62e0 100644 --- a/cpp/prevoi19_robots.cpp +++ b/cpp/prevoi19_robots.cpp @@ -1,5 +1,6 @@ #include #include +#include #include using namespace std; @@ -7,9 +8,6 @@ typedef long long ll; struct Point { ll x, y; - - Point(ll x = 0, ll y = 0) : x(x), y(y) { - } }; ll manhattan(const Point &a, const Point &b) { @@ -24,55 +22,46 @@ bool isPossible(ll D, const vector &chargers, const Point &start, ll N) { ll min_v = v0 - N; ll max_v = v0 + N; - vector test_points; - - vector u_candidates = {min_u, max_u}; - vector v_candidates = {min_v, max_v}; + vector u_candidates = {min_u, max_u, u0}; + vector v_candidates = {min_v, max_v, v0}; for (const Point &c: chargers) { ll cu = c.x + c.y; ll cv = c.x - c.y; - u_candidates.push_back(cu - D); u_candidates.push_back(cu + D); v_candidates.push_back(cv - D); v_candidates.push_back(cv + D); } - u_candidates.push_back(u0); - v_candidates.push_back(v0); + sort(u_candidates.begin(), u_candidates.end()); + u_candidates.erase(unique(u_candidates.begin(), u_candidates.end()), u_candidates.end()); + + sort(v_candidates.begin(), v_candidates.end()); + v_candidates.erase(unique(v_candidates.begin(), v_candidates.end()), v_candidates.end()); for (ll u: u_candidates) { for (ll v: v_candidates) { - if ((u + v) % 2 == 0) { - ll x = (u + v) / 2; - ll y = (u - v) / 2; - test_points.push_back(Point(x, y)); - } else { - ll x1 = (u + v) / 2; - ll y1 = (u - v) / 2; - test_points.push_back(Point(x1, y1)); + if (u < min_u || u > max_u || v < min_v || v > max_v) continue; - ll x2 = (u + v + 1) / 2; - ll y2 = (u - v - 1) / 2; - test_points.push_back(Point(x2, y2)); + ll x = (u + v) / 2; + ll y = (u - v) / 2; + + if ((u + v) % 2 != 0) continue; + + Point p = {x, y}; + if (manhattan(p, start) > N) continue; + + bool valid = true; + for (const Point &c: chargers) { + if (manhattan(p, c) < D) { + valid = false; + break; + } } + if (valid) return true; } } - - for (const Point &p: test_points) { - if (manhattan(p, start) > N) continue; - - bool valid = true; - for (const Point &c: chargers) { - if (manhattan(p, c) < D) { - valid = false; - break; - } - } - if (valid) return true; - } - return false; } @@ -97,7 +86,6 @@ int main() { while (left <= right) { ll mid = left + (right - left) / 2; - if (isPossible(mid, chargers, start, N)) { left = mid + 1; } else {