DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Minimum Perimeter Triangle

Back to problem

Editorial

Sort the points by their xxx-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 δ\deltaδ be the smaller of the two answers. It remains to check whether a triangle using points from both halves has perimeter smaller than δ\deltaδ.

Every side of a triangle whose perimeter is smaller than δ\deltaδ has length smaller than δ2\frac{\delta}{2}2δ​. If the side lengths are a,b,ca,b,ca,b,c, the three points are not collinear, so

a<b+c.a<b+c.a<b+c.

Thus,

2a<a+b+c<δ.2a<a+b+c<\delta.2a<a+b+c<δ.

Consider a triangle using points from both halves. Two vertices from different halves are less than δ2\frac{\delta}{2}2δ​ apart, so every vertex lies in the vertical strip whose horizontal distance from the dividing line is less than δ2\frac{\delta}{2}2δ. Since every side is shorter than , the range of the three -coordinates is also smaller than .

Therefore, sort the points in the strip by yyy-coordinate and only consider points whose yyy-coordinate difference is smaller than δ2\frac{\delta}{2}2δ​.

Only a constant number of points can lie in such a range. Consider any horizontal portion of the strip of height δ2\frac{\delta}{2}2δ​. The parts to the left and right of the dividing line are rectangles of width and height at most δ2\frac{\delta}{2}2δ.

Divide each rectangle into 222 columns and 333 rows. Each small rectangle has width δ4\frac{\delta}{4}4δ​ and height δ6\frac{\delta}{6}, so its diagonal is

(δ4)2+(δ6)2=1312δ.\sqrt{\left(\frac{\delta}{4}\right)^2+\left(\frac{\delta}{6}\right)^2} = \frac{\sqrt{13}}{12}\delta.(4δ​)2+

If one small rectangle contained three points from the same recursive half, those points would form a triangle of perimeter at most

3⋅1312δ=134δ<δ,3\cdot\frac{\sqrt{13}}{12}\delta = \frac{\sqrt{13}}4\delta <\delta,3⋅1213​​

contradicting the recursive answer for that half.

Hence each small rectangle contains at most two points. There are 666 small rectangles on each side, so at most 121212 points lie on each side and at most 242424 points lie in total.

Thus, while scanning the strip in yyy-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 (δ2)2\left(\frac{\delta}{2}\right)^2(2δ​)2. 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 yyy are merged in linear time. Therefore, the total time complexity is

O(Nlog⁡N),O(N\log N),O(NlogN),

and the memory complexity is O(N)O(N)O(N).

Solution written by GPT5.6

​
δ2\frac{\delta}{2}2δ​
yyy
δ2\frac{\delta}{2}2δ​
​
6δ​
(6δ​)2
​
=
1213​​δ.
δ
=
413​​δ<
δ,