Divide and Conquer: Quoit Design and the Closest Pair
On October 30 I worked through Quoit Design from this contest. The public statement is HDU 1007. Every toy is a point, and the ring should be as large as possible without encircling two toys at once. I did not start by picking an algorithm. I asked what actually limits the radius.
A toy is encircled when its distance to the center is strictly less than the radius. If two points are less than apart, some circle of radius contains both of them strictly: put the center at the midpoint. The radius can therefore be at most half the distance of the closest pair. If two toys coincide, the radius is 0. The output keeps two decimal places.
For and , the Euclidean distance is
Let be the minimum over all pairs. The answer is .
While comparing pairs, I can compare squared distances, . The square root is increasing on nonnegative numbers, so the pair with the smallest square is the closest pair. The square root and the division by 2 wait until the radius is printed.

Turn the Radius into the Closest Pair
After that reduction, the problem is the closest pair of points in the plane. can be , and the value to find is .
A half with fewer than two points has no pair, so it does not enter the minimum. If a duplicate pair has already made the distance 0, the strip does not need to be searched.
Why Checking Every Pair Is Not Enough
My first idea was to measure every pair, each pair once:
The time is . At , the number of pairs is
That is nearly five billion comparisons. The distance formula is not the expensive part. The number of pairs is.
Sorting and Pruning Do Not Change the Worst Case
I then sorted the points by . Let be the best distance so far. Once a later point satisfies , its Euclidean distance from is at least that large. Later points are even farther in , so the inner loop can stop.
This skips some comparisons. It does nothing when every point has the same and the points differ only in : the gap stays 0, and the worst case is still .
Doing fewer comparisons inside a loop is not the same as improving the worst-case bound.
The Halves Are Easy, and the Merge Cost Is the Hard Part
Pruning did not change the worst case. At I needed a way to reduce the number of pairs in the whole problem.
The points are already sorted by , so a cut down the middle leaves about half the points on each side. Each half asks the same question: how close is the closest pair inside this half? The question is unchanged and the size is smaller, so the same procedure can run recursively. The closest distance on the left and the closest distance on the right both come straight out of that recursion. Each half’s is the easy part.
What is still open is the cost of putting the two halves together. Take the smaller of the two results:
That only constrains pairs inside one half: points on the left are at least apart, and so are points on the right. One point on the left and one point on the right can still be closer, so the answer cannot stop at this . The merge still has to handle pairs that cross the cut.
That merge cost is the hard part. If every left point is compared with every right point, one level is , and splitting the set has not removed any work. The rest of the argument is about how many crossing pairs still have to be checked once is known, and whether that level can be brought down to .
Only the Strip Near the Cut Matters
Let the cut be . If a pair is closer than , its gap is also less than , because . A crossing pair that might update the answer has to lie in the vertical strip
The strip has width .
The strip can still hold many points. Scan them by increasing . For a fixed point , a later point with cannot be closer than , and neither can any point after it. A candidate must satisfy both
A candidate might be closer than . It is not already known to be closer.
Why Each Point Has Only a Constant Number of Candidates
This was the part that took the longest. The known fact is that pairs inside one half are at least apart. Pairs across the cut are not known to be that far. If they were, the merge would be unnecessary. I cannot claim that every pair in the whole strip is at least .
For a fixed , take the rectangle from to and from to . It is wide and tall. Split it into 4 columns and 2 rows, eight squares of side . A cell’s edge may lie on the cut, but its interior does not contain both halves: a point exactly on the cut was assigned to one half when the set was split.

Two points in the same cell are at most a diagonal apart:
The chain is:
- Two points in one cell are less than apart.
- That cell belongs entirely to one half.
- Two points in the same half cannot be less than apart.
- Therefore one cell cannot contain two points.
Eight cells and one point per cell give at most eight points in the rectangle. One of them is , so at most seven candidates follow it. Each point is compared with a constant number of successors, not with every point in the strip.
The cells do not directly limit distances across the cut. They prove that the number of candidates has a fixed upper bound.
The Rectangle Is Rebuilt for the Current Point
I also asked whether could only slide along the bottom edge of one fixed rectangle.
The strip is fixed by the cut and the current . The local rectangle is drawn separately for each current point. Scanning by increasing , a fixed only needs later points, so the bottom edge is and the top edge is . The next point gets a new bottom edge. is not moving along one shared edge.
Scanning downward uses a rectangle that extends the other way. The direction is not the point. The sort is what lets points whose gap is already at least be skipped.
How the Time Bound Becomes N log N
The whole procedure sorts by , splits into halves, finds and , checks crossing pairs through the strip and the order, and keeps the smaller distance. The radius is half of that distance.
Sorting again at every recursive level can cost per level and overall. For , I can prepare an -ordered sequence and a -ordered sequence at the start. Each call splits the -ordered sequence into the two halves, then merges those ordered sequences on the way back. The strip scan and the at most seven comparisons then run linearly in that order.
The merge at one level is , so
and . The initial sort is the same order of growth and does not raise the bound.
Review
I did not start from divide and conquer. Checking every pair is correct, and there are too many pairs. Stopping the inner loop after an sort removes some work without changing the worst case. What divide and conquer adds is the distance already found in the two halves: points far from the cut can be ignored, points whose gap is at least can be ignored, and each point has only a constant number of candidates left.
The sentence I want to keep is this: split the problem, then use the subproblem results to prove which pairs need not be checked. That is what makes the merge affordable.
Added on November 1: I later tried to turn this problem into a vibe-style explainer video. The models and the cost are in An Attempt at a Vibe Knowledge Video.