Editorial for DMOPC '21 Contest 2 P1 - Bosses


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.

Author: Riolku

Subtask 1

Observe that it is always optimal to grow to our final height at the beginning. Also, note that 0 and the elements of Ai are the only heights we need to consider. The intuitive proof of this is that it is always better to grow more or not to grow at all, depending on H and P.

We can simulate this subtask in quadratic time.

Time Complexity: O(N2)

Subtask 2

We need a faster simulation. Consider a given height x. Let ac be the number of elements with larger height than x in A. Let at be the total height of the elements with larger height than x in A. Then we need to pay Hx to grow to this height, and P(atxac) as our potion total. The easiest solution is to insert a zero into the list, sort, and iterate backward.

Time Complexity: O(NlogN)

Proof of Claim

Note that since we grow to our final height at the beginning, the order of the bosses is arbitrary. Sort them. As in the second subtask, define at and ac. Consider an arbitrary height x between Ai and Ai+1. While our height is between these two values, at and ac don't choose. Consider our expression of cost from subtask 2 Hx+P(atxac):

C=Hx+PatPacx=(HPac)x+Pat

Depending on how H compares to Pac, we either benefit from continually increasing x, or continually decreasing it, until at and ac change.


Comments

There are no comments at the moment.