Rainforest Jump
Time limit3sMemory limit1024 MB
Given distinct tree heights, find the fewest jumps to the nearest taller tree that get from some tree in one range to some tree in a later range, or report it is impossible.
- Level
Hard8 of 10
- Topics
- Tree, Union-find, Array
- Solved
- No attempts yet
Problem
In a tropical rainforest in Sumatra, there are trees numbered from 0 to from left to right. All trees have different heights. The height of tree is .
A professor is training orangutans to move by jumping between trees. In one jump, an orangutan at the top of its current tree can jump left or right to the nearest tree that is taller than the current tree. Precisely, an orangutan at tree can jump to tree if and only if one of the following holds:
- , and is the largest nonnegative integer less than with this property.
- , and is the smallest nonnegative integer greater than with this property.
The professor has plans for the orangutans. Each plan is given by four integers , , , (). The professor wants to know whether an orangutan can start at some tree () and, through jumps, finally reach some tree (). If it is possible, the professor wants the orangutan to complete the plan with the fewest possible jumps.
Constraints
- (for all )
- (for all )