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.
We model the problem as a shortest path problem in a graph in which the state is an ordered pair
, where
is the square at which Nikola is located and
is the length of the preceding jump.
From state
Nikola can move to state
moving backwards, or to state
moving forwards, assuming these jumps don't move him out of the playing area.
Notice that the graph is acyclic (meaning that there is no way to visit a state twice while traversing the graph), because forward jumps increase the
parameter. Because of this, the length of the shortest path can be calculated with dynamic programming.
Let the function
represent the smallest cost for Nikola to get from square
to square
, if the preceding jump was of length
.
Here's how to calculate the value of the function:
- For
or
,
;
- For
,
;
- Otherwise,
![\displaystyle opt(square, jumplen) = fee[square]+\min\{opt(square-jumplen, jumplen), opt(square+jumplen+1, jumplen+1)\}](//static.dmoj.ca/mathoid/386cf28acc3e3f5e4f8cd8e3187efcb12033b71e/svg)
The solution is then
.
Comments