Hot Dogs in Manhattan
Time limit2sMemory limit128 MB
Given existing stands on a w by h grid, choose two empty intersections maximizing the smaller of the two minimum distances to all other stands.
- Level
Hard8 of 10
- Topics
- Binary search, Geometry, Brute force, Math
- Solved
- No attempts yet
Problem
Two friends, Barack and Mitt, each want to open a hot dog stand in Manhattan, and they are looking for the two best locations.
Both want to place their stand at an intersection for maximum exposure. Manhattan already has many existing stands, all located at intersections. A stand placed close to another stand (including the other friend's new stand) attracts fewer customers, so they want their stands to be as far as possible from every other stand.
Model Manhattan as a finite square grid of vertical streets and horizontal streets. Vertical streets are at and horizontal streets at . Consecutive parallel streets are one unit apart, so the distance between intersections and is .
The privacy of an intersection is the minimum distance from it to every other stand. Once both new stands are placed, each new stand also counts as a stand for the other, so the privacy of Barack's location depends on the distance to Mitt's location and vice versa. Barack and Mitt want to choose two intersections so that the smaller of their two privacies is as large as possible. Output that maximum value.
Input
The first line contains one positive integer: the number of test cases, at most . Each test case is given as follows:
- One line with three space-separated integers , , and ( and ): the number of existing stands and the number of vertical and horizontal streets.
- lines, each with two space-separated integers and ( and ): the intersection of the -th existing stand.
All existing stands are at distinct intersections, and at least two intersections have no stand.
Output
For each test case, output one line with a single integer: the maximum privacy that both Barack and Mitt can simultaneously achieve.
Note
In the first test case there is one existing stand at on a grid. Placing the two new stands at and gives each a privacy of : both are at distance at least from the existing stand, and they are at distance from each other. No placement does better, so the answer is .
When there is no existing stand, the two new stands can be put at opposite corners, so the answer is simply .