Editorial for Yet Another Contest 3 P3 - Topography
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Let be the sequence of elevations.
Without minimizing the maximum elevation
We can mostly ignore the constraint that all elevations are positive, provided our elevations are not too far apart. This is because once we have generated a sequence satisfying the requirements, we can increase or decrease each element of by the same amount such that all elements of are positive.
Let us define a sequence such that . In other words, is the difference array of . Note that the requirement that is increasing is equivalent to the requirement that are positive. Similarly, the requirement that is decreasing is equivalent to the requirement that are negative. Finally, since no two adjacent elements of can equal, cannot be present in sequence .
For the solution without minimizing the maximum element of , it suffices to set the corresponding elements of to for each increasing requirement, and all other elements of to . We can then reconstruct as the prefix sum sequence of , and apply the required shift such that all elements of are positive. If there are two conflicting requirements such that an element of must be both positive and negative, then there is no solution.
Minimizing the maximum elevation
Now, we will try to find a solution which minimizes the maximum element of . As we will see later, there is an edge case regarding whether the maximum elevation can be . Depending on the implementation, it may be easier to initially test whether or are valid sequences of . The rest of the solution handles the case where these sequences are invalid (and that the requirements do not contradict each other so that a solution exists).
Furthermore, let us merge overlapping intervals of the same type. For example, if we know that subarray from to is increasing, and that the subarray from to is increasing, then the subarray from to is increasing.
Let be the maximum value of amongst all merged requirements. Clearly, no solution with a maximum elevation of less than exists.
We conjecture that there is a solution with a maximum elevation of . We will process the requirements from left to right. For each increasing requirement between and , we can set the corresponding subarray to any increasing sequence such that and . For each decreasing requirement between and , we can set the corresponding subarray to any decreasing sequence such that and . For any element of which has not yet been determined, we can set it to any value which is not equal to its neighbours; this is always possible since we have at least unique values to work with.
However, this solution is flawed due to an edge case. If the subarray is increasing and the subarray is decreasing, then our algorithm would return , which is not allowed. Specifically, our algorithm is incorrect when two requirements have two adjacent subarrays and are of different types. Then, we should replace our earlier construction with the following:
Instead of making all increasing subarrays span between and , for each subarray from left to right, we should try to construct any increasing subarray such that the final element of the subarray is minimized. Similarly, for all decreasing subarrays, we should try to construct any decreasing subarray such that the final element of the subarray is as large as possible. If the leftmost element of the subarray has already been determined when we process it, then we can overwrite this element. All elements still have to be between and .
This construction handles all cases where there exists a solution with the maximum element equal to . However, this is not always possible, as demonstrated by the previous edge case. Hence, if our construction fails, we should repeat the construction, but with the condition that all elements have to be between and . Note that we are always guaranteed that a solution with the maximum element equal to exists; for example, ignoring overwriting values, we could enforce that for each increasing requirement between and , we can set the corresponding subarray to any increasing sequence such that and , and for each decreasing requirement between and , we can set the corresponding subarray to any decreasing sequence such that and .
Subtask was designed to reward naïve implementations which run in time. This can be sped up using a sweepline to solve subtask in or time.
Comments