Editorial for DMOPC '21 Contest 4 P6 - Balanced Line
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
For subtask 1, note that if our line has slope-intercept form after rotating the plane, then the balance value is equal to
where are the rotated points. Thus if we precompute the sum of the 's and the sum of the 's, then we can evaluate the balance value of any line in . We can now check all pairs of points for each query and take the smallest balance value.
Time complexity:
Subtask 2
For subtask 2, note that we can rearrange the balance value to
which is equal to times the vertical distance from the centroid of the points to the line. We can now visualize each query as drawing two rays coming out of the centroid at directions and , and we want to know the first time one of these two rays hits a line formed by two of the points.
Consider fixing one of the points on the optimal line. Then the lines formed by and one of the other points cut the plane into several pieces, and the centroid must lie in one of them. We can see that only the two lines that border this piece might be the optimal line. We can find these two candidate lines by looping through the other points and taking the two with the closest slope to as the centroid. Now we have only lines to check for each query, which can be done in total.
Be careful to handle the corner case where the centroid lies on a line that becomes vertical after rotation, as in sample 2.
Time complexity:
Subtask 3
For subtask 3, we need to do better than linear time for each query. Note that the lines cut the plane into several convex regions. We can find the border of the region containing the centroid with a "circular" convex hull trick and then process all the queries with an angular sweep through the convex hull.
Again, be careful to handle the corner case where the centroid lies on a line that becomes vertical after rotation, as in sample 2.
Time complexity:
Subtask 4
For subtask 4, we need to do better than looping through all pairs of the points. Consider sorting the points by their slope around the centroid. Then it can be shown that the optimal line is one of the pairs of adjacent points in this ordering (including the pair formed by the first and last point). Thus we can obtain candidate lines by sorting the points once, and we can try all of them for each query.
Time complexity:
Subtask 5
For subtask 5, we can combine the ideas in subtasks 3 and 4 to build the circular hull from the candidate lines.
Time complexity:
Comments
I think you need to tiebreak the ordering by distance, or else this is not necessarily true in the case where you have a vertical line passing through the centroid.
Not sure if this is due to the test data being weak or the bound on the maximum coordinate, but answering each query in is sufficient to pass. It seems that no test has a hull of size .