Baltic OI 2012 - Mobile

Author: Andi Qu

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:

Hack

Without sorting, wouldn't be part of the union since . However, the actual union contains all three segments.

But what if completely covers ?

Antihack

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 .

Circles

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.

(There is also an solution - try to come up with it yourself!)

Code

1#include <bits/stdc++.h>
2#define x first
3#define y second
4using namespace std;
5
6pair<long long, long long> p[1000000];
7
8int main() {
9 ios_base::sync_with_stdio(0);
10 cin.tie(0);

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!

Give Us Feedback on Baltic OI 2012 - Mobile!