Editorial
Sort the points by their -coordinates and apply divide and conquer.
Split the current set into a left half and a right half, and recursively compute the minimum perimeter in each half. Let be the smaller of the two answers. It remains to check whether a triangle using points from both halves has perimeter smaller than .
Every side of a triangle whose perimeter is smaller than has length smaller than . If the side lengths are , the three points are not collinear, so
Thus,
Consider a triangle using points from both halves. Two vertices from different halves are less than apart, so every vertex lies in the vertical strip whose horizontal distance from the dividing line is less than . Since every side is shorter than , the range of the three -coordinates is also smaller than .
Therefore, sort the points in the strip by -coordinate and only consider points whose -coordinate difference is smaller than .
Only a constant number of points can lie in such a range. Consider any horizontal portion of the strip of height . The parts to the left and right of the dividing line are rectangles of width and height at most .
Divide each rectangle into columns and rows. Each small rectangle has width and height , so its diagonal is
If one small rectangle contained three points from the same recursive half, those points would form a triangle of perimeter at most
contradicting the recursive answer for that half.
Hence each small rectangle contains at most two points. There are small rectangles on each side, so at most points lie on each side and at most points lie in total.
Thus, while scanning the strip in -order, it is sufficient to try every triangle formed by each point and the constant number of following points. Triangles whose three vertices belong to the same recursive half have already been handled recursively and may be skipped.
Before evaluating a perimeter, the implementation first checks whether all three squared side lengths are smaller than . Square roots are computed only for triples passing this filter, which significantly improves the constant factor.
At every recursion level, the two lists sorted by are merged in linear time. Therefore, the total time complexity is
and the memory complexity is .
Solution written by GPT5.6