Baltic OI 2012 - Mobile
Authors: Andi Qu, Ryan Chou
If we can cover the highway using circles of radius , then we can also cover the highway using circles of radius . This suggests that we should binary search for the answer.
Checking a Radius
If a circle covers a part of a highway, it covers a line segment. Using the Pythagorean theorem (), we can find the coordinates of the endpoints of each such segment. Let the segment from the -th tower be .
Checking whether a radius is good now becomes checking whether the union of these segments is a single segment that completely covers the highway.
We can solve this in time by sorting the segments by their left endpoints and then merging each segment into the union if it intersects the union of the previous segments.
Unfortunately, this is just a bit too slow ( in total) and only scores 50 points.
Luckily, we don't have to sort the segments!
A Nice Property
Why would we need to sort the segments in the first place?
Imagine we had three segments , , and in that order in the unsorted list. Watch what happens when these segments are arranged as shown below:
Without sorting, wouldn't be part of the union since . However, the actual union contains all three segments.
But what if completely covers ?
In this case, it doesn't matter that isn't a part of the union, since anyway.
The nice thing about this particular problem is that if we have segment coming after segment in the list but 's left endpoint is to the left of 's left endpoint, then must completely cover .
Why is this true? The only time we will have segment coming after segment in the list but 's left endpoint is to the left of 's left endpoint is when and .
Clearly, completely covers . We can therefore apply our algorithm for a sorted list and still get the correct answer!
This gets rid of the factor in our original complexity and so the final complexity is , which is good enough for 100 points.
Implementation
#include <bits/stdc++.h>#define x first#define y secondusing namespace std;pair<long long, long long> p[1000000];int main() {ios_base::sync_with_stdio(0);cin.tie(0);
Faster Solution
Although not necessary for full credit, there also exists an solution.
Try to come up with it yourself before moving on!
Faster Solution
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!