Editorial for WC '15 Contest 3 S1 - Energy Absorption


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

For this problem, we want to keep Batman's net damage positive, given that his initial damage is Q_i (positive). So, we want to determine after how many punches this will happen, or if it will ever happen. The problem is, we have to be able to compute the answer quickly for many different initial damages.

Start by observing that if Superman hasn't yet stopped punching for some index and the next punch will do positive damage, then Superman is guaranteed to follow through with this punch. This is because if Batman's damage is currently positive, then positive damage will always make it more positive, and so there's no reason for Superman to stop. This point is true regardless of the initial damage of Batman. That leads to the observation that only on particular indices will Superman ever stop, regardless of the initial damage of Batman.

We must first find these "key indices" in the sequence of punches – all indices i such that the total damage dealt by the first i punches (D_1+D_2+\dots+D_i) is less than any earlier such total (less than the total damage dealt by the first j punches for any j < i). These key indices represent the punches which could possibly cause Batman's damage to become non-negative, meaning that Superman will always stop punching right before one of them (or after all N punches). In particular, if Batman starts with Q_i damage, then we need to find the first key index k such that its total damage D_1+D_2+\dots+D_k is strictly smaller than -Q_i, and we'll know that Superman will perform precisely k-1 punches. If there's no such key index, then Superman will perform all N punches.

Iterating over the key indices for each query to find the appropriate one results in an \mathcal O(NM) algorithm, which is too slow for full marks. However, the key indices have cumulative damages which are monotonic (strictly decreasing), so we can instead use binary search to improve the time complexity to \mathcal O(M \log N).


Comments

There are no comments at the moment.