Editorial for WC '18 Contest 3 J4 - Leveling Up


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.

Let Z be the maximum possible position (Z=100000), and let WMp and WGp be the M and G values of the wild Pokémon at position p (if any), with WMp=WGp=0 if there's no such Pokémon. Then, WM1,,WMZ and WG1,,WGZ may be populated in O(Z+N) time by iterating over the N Pokémon.

We'll greedily simulate Jessie and Arbok's training process. Let p1 be the smallest position which Jessie can currently freely reach, p2 be the largest one, and a be Arbok's current level. Initially, p1=p2=S and a=L.

At any point in time, if p111 and aWMp11, then Jessie is able to expand her reachable range to encompass position p11, and has no reason not to do so. Therefore, we can increase a by WGp11, and then decrement p1. Similarly, if p2+1Z and aWMp2+1, then Jessie might as well expand her reachable range to encompass position p2+1. Therefore, we can increase a by WGp2+1, and then increment p2.

We should repeat the above process of expanding Jessie's range of reachable positions left and right whenever possible until we arrive at a situation in which it cannot be expanded in either direction. At that point, nothing more can be done, and we can proceed to output the current value of a.

The time complexity of the above solution is O(Z+N). A similar algorithm running in O(NlogN) time is also possible, if we sort and consider only positions of interest (Jessie's starting position as well all wild Pokémons' positions) instead of all Z possible positions.


Comments

There are no comments at the moment.