Editorial
Compute the convex hull of all points and let its area be .
If the convex hull is a segment, sort the points lexicographically by and output that order. Now suppose the convex hull has positive area.
Set the strip spacing to and let . Use the following two horizontal grids: and , where is an integer.
For each grid, build one candidate tour as follows.
- Assign every point to its nearest grid line.
- Process the grid lines from bottom to top. If the grid-line index is even, visit the assigned points in increasing order of ; if it is odd, visit them in decreasing order of .
- Connect the last point back to the first point.
Compute the lengths of the two candidate tours and output the shorter one. The convex hull and its area can be computed in time, so the whole algorithm runs in time.
Proof
Let , , and be the horizontal width, perimeter, and diameter of the convex hull. We have .
Fix one grid. Project every point vertically onto its assigned horizontal line. Expand the convex hull upward and downward by . Every projected point lies inside the expanded convex body. Its area is , its perimeter is , and its diameter is at most .
Solution written by GPT5.6