Editorial for Wesley's Anger Contest 6 Problem 7 - SantaTracker++
Submitting an official solution before solving the problem yourself is a bannable offence.
We can see that the distance squared from a house at to a point on the -axis is . Since the distance squared to each house is a quadratic in vertex form with a leading coefficient of , each quadratic is a translation of another. Thus, it must be that each house can be closest to the -axis over at most one continuous interval. We can keep track of the current left endpoint (starting at ) and binary search for the first value of (up to ) which has a different house that is closest to the -axis at that point. We can do this by looping through each of the houses and calculating the distance using the previously given formula. We can then move the left endpoint to that value of and repeat. From here, we can compute the required percentage for each house.
There are alternative solutions for this subtask.
For the second subtask, we can see that does not matter. The only part of each house that can be closest to the -axis is the bottom line segment at where . Unfortunately it is no longer the case that each house can be closest to the -axis over at most one continuous interval.
On the -axis, we can see that if , the closest point of the house to the -axis is . When , the closest point of the house to the -axis is . When , the closest point of the house to the -axis is . Consider all sorted distinct values of and . Suppose we have two adjacent values and . In the interval we can see that the equation of distance squared the closest point of each house remains constant. It is either a constant or a quadratic in vertex form with a leading coefficient of . Thus, within each interval, each house can be closest to the -axis over at most one continuous segment. We can perform the same algorithm as subtask 1 for each interval. Since the total number of changes of the closest house when moving from to is , this will not affect the time complexity.
If we rearrange the equation of the house to the -axis to standard form, we obtain . We can see that each equation has the same quadratic term of . We want to be able to determine for each quadratic, which continuous range of values is it the minimum. We can do this by maintaining the convex hull of the quadratics. To simplify this, if we only consider the linear and constant terms, we have the line . This does not affect the intersection of the quadratics which is relevant when computing the convex hull. After we compute the convex hull, we can then determine the points of intersection between each pair of adjacent lines in the hull. From these points of intersection, we can determine the ranges where each line is the minimum and compute the percentages from there.
Similar to subtask 2, we can consider each house as the two points and , and the line at . We can compute the convex hull for each of the points and compute the ranges for each quadratic. For each of the lines, we can perform a line sweep to compute a second set of ranges of the closest lines (in terms of distance squared) to the -axis. We can then combine these ranges using a two pointer approach. For a quadratic and a horizontal line, there could be at most three resulting segments: a segment where the horizontal line is the minimum, a segment where the quadratic is the minimum, and another segment where the line is the minimum. We can compute these segments using the quadratic formula to find the intersection of a horizontal line and a quadratic. After computing the combined range, the percentages can be computed from here.