Skip to content

Divide and Conquer: Quoit Design and the Closest Pair

The largest quoit radius is half the closest-pair distance. This note follows why divide and conquer does not have to check every 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 2r2r apart, some circle of radius rr 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 Pi=(xi,yi)P_i=(x_i,y_i) and Pj=(xj,yj)P_j=(x_j,y_j), the Euclidean distance is

dij=(xi−xj)2+(yi−yj)2. d_{ij}=\sqrt{(x_i-x_j)^2+(y_i-y_j)^2}.

Let dmin⁡d_{\min} be the minimum over all pairs. The answer is r=dmin⁡/2r=d_{\min}/2.

While comparing pairs, I can compare squared distances, (xi−xj)2+(yi−yj)2(x_i-x_j)^2+(y_i-y_j)^2. 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.

Quoit Design: the largest radius is half the closest distance, and the merge checks the strip
The closest pair sets the radius. Across the cut, only a few points in the strip are compared

Turn the Radius into the Closest Pair

After that reduction, the problem is the closest pair of points in the plane. NN can be 100000100000, and the value to find is dmin⁡d_{\min}.

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:

for (int i = 0; i < N; i++) {
    for (int j = i + 1; j < N; j++) {
        // update the minimum with the squared distance
    }
}

The time is O(N2)O(N^2). At N=100000N=100000, the number of pairs is

N(N−1)2=4999950000. \frac{N(N-1)}{2}=4999950000.

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 xx. Let dd be the best distance so far. Once a later point PjP_j satisfies xj−xi≥dx_j-x_i\ge d, its Euclidean distance from PiP_i is at least that large. Later points are even farther in xx, so the inner loop can stop.

This skips some comparisons. It does nothing when every point has the same xx and the points differ only in yy: the xx gap stays 0, and the worst case is still O(N2)O(N^2).

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 N=100000N=100000 I needed a way to reduce the number of pairs in the whole problem.

The points are already sorted by xx, 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 dmin⁡d_{\min} is the easy part.

What is still open is the cost of putting the two halves together. Take the smaller of the two results:

d=min⁡(dL,dR). d=\min(d_L,d_R).

That only constrains pairs inside one half: points on the left are at least dd 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 dd. 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 O(N2)O(N^2), 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 dd is known, and whether that level can be brought down to O(N)O(N).

Only the Strip Near the Cut Matters

Let the cut be x=xmx=x_m. If a pair is closer than dd, its xx gap is also less than dd, because ∣xi−xj∣≤dij|x_i-x_j|\le d_{ij}. A crossing pair that might update the answer has to lie in the vertical strip

∣x−xm∣<d. |x-x_m|<d.

The strip has width 2d2d.

The strip can still hold many points. Scan them by increasing yy. For a fixed point PP, a later point QQ with yQ−yP≥dy_Q-y_P\ge d cannot be closer than dd, and neither can any point after it. A candidate QQ must satisfy both

{∣xQ−xm∣<d,0≤yQ−yP<d. \begin{cases} |x_Q-x_m|<d,\\ 0\le y_Q-y_P<d. \end{cases}

A candidate might be closer than dd. 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 dd 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 dd.

For a fixed PP, take the rectangle from xm−dx_m-d to xm+dx_m+d and from yPy_P to yP+dy_P+d. It is 2d2d wide and dd tall. Split it into 4 columns and 2 rows, eight squares of side d/2d/2. 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.

Eight cells above P, with Q1 through Q7 in the other cells
P takes one cell, so at most seven candidates follow it. Two points in one cell would be less than d apart

Two points in the same cell are at most a diagonal apart:

(d2)2+(d2)2=d2<d. \sqrt{\left(\frac{d}{2}\right)^2+\left(\frac{d}{2}\right)^2}=\frac{d}{\sqrt{2}}<d.

The chain is:

  1. Two points in one cell are less than dd apart.
  2. That cell belongs entirely to one half.
  3. Two points in the same half cannot be less than dd apart.
  4. 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 PP, 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 PP could only slide along the bottom edge of one fixed rectangle.

The strip is fixed by the cut and the current dd. The local rectangle is drawn separately for each current point. Scanning by increasing yy, a fixed PP only needs later points, so the bottom edge is y=yPy=y_P and the top edge is y=yP+dy=y_P+d. The next point gets a new bottom edge. PP 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 yy gap is already at least dd be skipped.

How the Time Bound Becomes N log N

The whole procedure sorts by xx, splits into halves, finds dLd_L and dRd_R, checks crossing pairs through the strip and the yy order, and keeps the smaller distance. The radius is half of that distance.

Sorting again at every recursive level can cost O(Nlog⁡N)O(N\log N) per level and O(Nlog⁡2N)O(N\log^2 N) overall. For O(Nlog⁡N)O(N\log N), I can prepare an xx-ordered sequence and a yy-ordered sequence at the start. Each call splits the yy-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 O(N)O(N), so

T(N)=2T(N2)+O(N), T(N)=2T\left(\frac{N}{2}\right)+O(N),

and T(N)=O(Nlog⁡N)T(N)=O(N\log N). The initial xx 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 xx sort removes some work without changing the worst case. What divide and conquer adds is the distance dd already found in the two halves: points far from the cut can be ignored, points whose yy gap is at least dd 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.

Note

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.