You are not logged in. Please login at to post your questions!


TWOROADS - Editorial


Problem Link:





High School Geometry


Given a set S of N points in plane, you have to draw two lines such that sum of squares of distance of each point from nearest line is smallest.


The first thing one realizes about this problem is that the business of taking minimum of the distances is causing all the trouble. If we had to draw a single line to optimize sum of squares of its distance from all points, it would be a piece of cake. High school calculus can be used to find the optimum equation of the line. Here is a brief sketch:

Let the equation of optimum line be y +mx +c = 0. The square of distance of a point (x0,y0) from this line is (y0 + mx0 + c)2/(1 + m2). Therefore, sum of squares of distance of all points is

(Y2 + m2X2 + c2 * N + 2 * m * XY + 2 * Y * c + 2 * m * X * c) / (1 + m2)
where Y2 = sumi yi2, X2 = sumi xi2, XY = sumi yixi, X = sumi xi, Y = sumi yi, N = sumi 1

We need to find optimum m and c. To do this, first differentiate with respect to c to get c as a (linear)function of m, plug it in the above, and differentiate with respect to m to get optimum value. Refer to solutions at the end for more precise details. Assuming that the one line case can be solved in constant time, given X, Y, XY, N, X2, Y2, we can now proceed.

Now imagine the optimal pair of lines. Let the lines be named A and B. Every point in our set S is closer to exactly one of A and B. Let SA be the set of points in our S closer to A than B. Similarly define SB. Note that SB = S - SA.

Now suppose, by some magic, we managed to find the set SA. Then we would again be done. This is because line A is the optimal line for the set SA, and line B is the optimal line for remaining points.

Cool ! So, we now have a O(N * 2N) solution, which basically iterates over all subsets SA of S, and reports the solution.

Obviously, the next step is to note that sets SA and SB cannot be arbitrary. If we are given a pair of lines A and B, then SA consists of exactly those points in S which do not lie in the colored region. Similarly, SB consists of those points which lie in the colored region. Justification is very straightforward and left to the reader. The colored region can be identified by a pair of perpendicular lines. For any two perpendicular lines L, M, let RL,M denote the the first and third quadrant of the co-ordinate system defined by line L, M. Formally, RL,M = {points P | tan ∠ PXL ≥ 0}, X being intersection point of L and M.

Due to discussion above, we know that the set SA cannot be arbitrary. The set SA has to be such that there exist two perpendicular lines L and M, so that SA = S ∩ RL,M.

In fact, we give a list Z of pairs of lines(i.e. Z={(L1, M1), (L2,M2), ... (LM,MM)}) such that, for any arbitrary pair of lines (L, M) we can obtain another pair of lines (L', M') which satisfies the following
* S ∩ RL,M = S ∩ RL',M'
* (L', M') ∈ Z

In other words, the possible lines L and M can be infinite in number, but we can find a finite set of pairs of lines which still induce all possible partitions of the given set S. In fact we can show that the required number of pairs of lines is polynomial in N.

Here is how to obtain L', M' from any L, M.

  • Translate L, parallel to itself towards right, until L hits some point P1 ∈ S. Stop translating when it is infinitesimally small distance away from P1.

  • Now translate M, parallel to itself towards right, until it hits some point P2 ∈ S. Again stop translating when distance between line and point becomes infinitesimally small.

  • Rotate both L and M clockwise so that i) M remains perpendicular to L, ii) One end point of L is fixed at P1, iii) One end point of M is fixed at P2. The intersection point of L and M moves in a circle with P1P2 as diameter. Stop rotating when one of L or M hits some point P3 ∈ S. Again, stop rotating when it is infinitesimally small distance away from P3.

Now it is clear that the new lines L', M' obtained by above process still induce same partition of S as L, M did. Moreover, L', M' are completely defined by
* Points P1, P2, P3.
* One bit indicating whether P3 was hit from above or below. It doesn't matter whether L' actually hit P3 or M' because they are interchangeable.

The set Z of all possible final pairs (L', M') obtained after translation/rotation has size 2n3, and can be easily enumerated. We can solve the problem in O(n^3) overall time as well, because the partitions imposed by these lines can be interconverted by adding/removing single points, and hence (X, Y, XY, X2, Y2) tuple can be re-calculated in constant time. See the solutions below for exact details.

Setter's Solution:

Can be found here

Tester's Solution:

Can be found here

Editorialist's Solution:

Can be found here

This question is marked "community wiki".

asked 16 Sep '13, 15:14

utkarsh_lath's gravatar image

5★utkarsh_lath ♦♦
accept rate: 0%

edited 16 Sep '13, 16:49

admin's gravatar image

0★admin ♦♦

When I first read the problem, I realized I had to find regression line of N points. In this case instead of one, I had to find two. I didn't know how to find one line, let alone two. So I googled and found an interesting paper on K-line mean :

In the paper, they describe an O(n^3) algorithms for 2-line mean. They use vectors and linear algebra to solve the problem (I think!). If only I was good at linear algebra then maybe I could have understood this article (all those matrix decomposition and vector products boggled my mind). I tried studying linear algebra books for few days but they didn't cover these advanced topics (SVD for example). Plus reading 500+ pages of a books didn't seem efficient.

So anybody here solved the problem using this paper or linear algebra? Any resource that will help me with linear algebra (and this problem).


answered 16 Sep '13, 17:26

forthright's gravatar image

accept rate: 10%

How you googled it ? I couldn't found any good topics

(17 Sep '13, 09:52) baukaman3★

look page 23, solution to our problem ))

(17 Sep '13, 09:54) baukaman3★

Another paper which solves a similar problem in O(N^3) can be found at

(17 Sep '13, 13:51) n2n_5★

I think approach to this problem is N^3logN.

Two outer loops gives us n^2. And inside it we have sorting(respect to projections to line) which is NlogN. Am I correct ?


answered 18 Sep '13, 13:21

baukaman's gravatar image

accept rate: 0%

Good spot ;-) @utkarsh_lath is that sorting necessary ?

(18 Sep '13, 15:57) betlista ♦♦3★

Yep, sorting is to be used, however, it seems complexity of solving one case(using calculus) is large enough that it hides the complexity of sorting.

(19 Sep '13, 06:34) utkarsh_lath ♦♦5★

Two consecutive projections only differ in two elements (a swap of two consecutive points), so maybe we can use only one sort and go clockwise, updating the sort order every time in O(1).

(14 Nov '13, 14:09) kevinsogo7★

Hi guys, can someone help me to find the roads for those three inputs?

-1 10
1 10
-3 12
3 12
-1 -10
1 -10


-1000 1000
0 999
1000 1000
-1 -1000
1 -1000


-1 -1000
1 -1000
-500 1000
500 1000
0 997
1000 999
1000 998
-1000 999
-1000 998

Testers solution fails for all the inputs. Setter's and editorialist's solution returns 0.414866845 for first input, 0.133333333 for second and 0.666666667 for the third, but it seems to me less than I'd expect. If possible, give me the roads in y = a*x + b form, thanks.


answered 17 Sep '13, 09:11

betlista's gravatar image

3★betlista ♦♦
accept rate: 11%


I noticed, that both setter's and editorialist's solution prints printf("%.9lf\n", ans/N); is that average instead of "minimum sadness" ?

(17 Sep '13, 09:20) betlista ♦♦3★

0.535184 * x + 1 * y + -10.1315 = 0
-7.28871 * x + 1 * y + 9.95538 = 0

0 * x + 1 * y + -999.667 = 0
0 * x + 1 * y + 1000 = 0

0 * x + 1 * y + -999 = 0
1 * x + 0 * y + -0 = 0

(17 Sep '13, 10:43) utkarsh_lath ♦♦5★

@utkarsh_lath thanks a lot

(17 Sep '13, 11:59) betlista ♦♦3★

@kevinsogo you are correct, negative coordinates are not valid input, but once you have working algorithm, it's not so important, but that's why tester's solution "fails"...

(17 Sep '13, 14:11) betlista ♦♦3★

If someone can fail this solution: Consider all lines that is pairwise combination of given points. For each line we will consider it's top and bottom parts. For each part we solve our known calculus thing. I got WA. Someone plz tell what test fails this approach ?


answered 17 Sep '13, 09:27

baukaman's gravatar image

accept rate: 0%


for my inputs, your solution returns


Can you describe your approach more precisely? As I understood, you split the points by line. In which part are point on that line?

(17 Sep '13, 09:39) betlista ♦♦3★

Yep , u r correct. I did all possible cases. Both to bottom(1 case). Both to top(1 case). one top one bottom(2 cases). So overall 4 cases for each pair of points.

(17 Sep '13, 09:45) baukaman3★

I have given best solution for @betlista 's inputs. Now you can check where you went wrong :)

(17 Sep '13, 10:46) utkarsh_lath ♦♦5★

Thanks a lot. I found bug in my calculus formula. Plus dividing by only one line will not work. It is evident for me now. It is great relief to find out where I was wrong.

(18 Sep '13, 09:31) baukaman3★

I'd like to ask for tips, how to solve such hard problem, respectively how to test them. First option is to create some test cases with known results. But even for those I asked here earlier, my expected result was wrong. Only idea I found is to find mean line for every triple of points, calculate distances so I will have some upper bound. I can do that for 50 points. Typically I implement some brute force solution to get answers for random test cases, but here I have no idea how to achieve this...


answered 17 Sep '13, 13:25

betlista's gravatar image

3★betlista ♦♦
accept rate: 11%

edited 17 Sep '13, 14:13


You could at least get a O(2^n) solution as described above. That is not too hard once you give a bit of thought to the problem.

The difficult part was to get it down to O(n^3). Probably the central idea was to realize the angle bisector thing. The rest of ideas used in our solutions is fairly common. When you need to bring down an infinite number of possibilities to a small finite number in geometrical setting, this method (method described in editorial) is the way to go.

(17 Sep '13, 14:25) utkarsh_lath ♦♦5★

The idea of splitting point between sets SA and SB is not difficult, but I got confused because when I choose points for SA and use mean line for those, maybe some point from SB are closer to that line, but now it seems to me, that this is no problem at all...

(17 Sep '13, 14:31) betlista ♦♦3★

Why does the answer always contain two intersecting lines? Isn't it possible to achieve better result with two parallel lines in some cases?


answered 25 Sep '13, 17:13

zennon's gravatar image

accept rate: 0%

Not always result are intersecting lines, see result for my 2nd test case...

(25 Sep '13, 17:26) betlista ♦♦3★

If the lines are not intersecting but parallel, the rest of the section, about two perpendicular lines that define the region closer to one line etc holds - with one modification: One of the two perpendicular lines lie far on the left, so that all points are on its right. The proof and everything is valid for that case as well.

(25 Sep '13, 20:56) utkarsh_lath ♦♦5★

Can anyone explain me why my solution finds better answer than the author's one?

Here is test and I guess the optimal partition looks like this. Strange thing is when I try to solve 1-line problem for these partition sets, my and author's solutions give the same total result, but it seems like author's solution doesn't find this partition when it solves the test mentioned above.

My answer is 16555.8836279 and author's is 16559.9967756. Here is my code by the way. I hope someone will explain me this.


answered 30 Sep '13, 01:08

zennon's gravatar image

accept rate: 0%

Hi zenon! Thank you for pointing this out, The problem here is the precision of float number. You put to much digits after the decimal point so our solution will not accurate anymore. Let say the double type can represent exactly x digits after decimal point (x depends on the range of the integer part also). Then if you use multiplication ie to calculate the projection then the input should have only x/2 digits after decimal point, Similarly, if you have product of 3 numbers somewhere in your program, then the i put should have x/3 digits after the point. In our test cases there is about 3-4 digits after the decimal point only. Actually we should inform that in the problem statement, sorry about that.


answered 04 Oct '13, 11:37

tuananh93's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 16 Sep '13, 15:14

question was seen: 3,279 times

last updated: 14 Nov '13, 14:09