Editorial for COCI '07 Contest 5 #5 Barica


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.

The core of the solution is a dynamic programming algorithm. To start with, sort the points in ascending order of x-coordinates, breaking ties in ascending order of y-coordinates.

Let dp(L) be the largest amount of energy Barica can have after getting from plant 1 to plant L.

Barica can reach plant L jumping to the right or upwards. As the length of the jump is insignificant, of all plants P with the same x-coordinate (and smaller y-coordinate) as plant L, we want the one we can reach with the largest amount of energy, that is for which dp(P) is largest. We apply the same reasoning to plants with the same y-coordinate.

Finally, dp(L) is calculated as the better of two cases (jumping upwards from the best plant with the same x-coordinate, or jumping to the right from the best plant with the same y-coordinate).

To reconstruct the frog's route, remember for each plant the best plant to have come from.


Comments

There are no comments at the moment.