Editorial for DMOPC '21 Contest 7 P4 - Rocket Race
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
The key observation is that after fixing the last rocket used to complete the race, the previous rockets can be determined greedily in decreasing order of . For this subtask, it is sufficient to explicitly maintain an array of the rockets sorted in decreasing order of , binary searching on the prefix sums of this array for each of the candidate last rockets. One detail to note is that the prefix sum array may not be in increasing order because could be negative, so binary search may fail to give the correct answer. To fix this, we may simply set for every single rocket. This is valid because the only case we would ever use a rocket where is as the last rocket, in which case the value of is of no significance.
Time Complexity:
Subtask 2
We can no longer loop through every possible last rocket. Instead, let's binary search on the minimum number of rockets that can be used to complete the race. Once again, we store the rockets in decreasing order of . If we consider using rockets, the maximum forward distance can be calculated by considering the two following cases:
- The last rocket is some rocket in the range . The maximum forward distance is then .
- The last rocket is some rocket in the range . The maximum forward distance is then .
All the required values can be precomputed in and accessed in , leaving us with time per query.
Time Complexity:
Subtask 3
We must now consider how to maintain the array stored in the previous subtask as updates are performed. There are many possible methods, but the simplest of them is to use a segment tree indexed by coordinate-compressed . If we store the number of rockets, sum of , maximum , and maximum in each node of the segment tree, we can obtain the required values for each binary search iteration by walking down the segment tree. This allows us to perform updates in and answer queries in , sufficient for full marks.
Time Complexity:
Remark: It is also possible to answer queries with a single walk down the segment tree obtaining time complexity, although this is not necessary at all given the lenient constraints on and .
Comments
can't figure how to answer case 2 by binary search
Do you mean the fact that the formula for the second case where the last rocket is in the range does not exhibit monotonic properties? In that case, we do not really mind, because the maximum forward distance is calculated by taking the maximum of case 1 and case 2. We can't get a worse answer using rockets instead of rockets because every rocket has a non-negative net distance traveled, remembering that we set from before.