Editorial for WC '18 Contest 3 J4 - Leveling Up
Submitting an official solution before solving the problem yourself is a bannable offence.
Let be the maximum possible position (
), and let
and
be the
and
values of the wild Pokémon at position
(if any), with
if there's no such Pokémon. Then,
and
may be populated in
time by iterating over the
Pokémon.
We'll greedily simulate Jessie and Arbok's training process. Let be the smallest position which Jessie can currently freely reach,
be the largest one, and
be Arbok's current level. Initially,
and
.
At any point in time, if and
, then Jessie is able to expand her reachable range to encompass position
, and has no reason not to do so. Therefore, we can increase
by
, and then decrement
. Similarly, if
and
, then Jessie might as well expand her reachable range to encompass position
. Therefore, we can increase
by
, and then increment
.
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 .
The time complexity of the above solution is . A similar algorithm running in
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
possible positions.
Comments