#P7599. [APIO2021] 雨林跳跃
[APIO2021] 雨林跳跃
Description
In the tropical rainforest on Sumatra Island, there are trees in a row. From left to right, they are numbered from to . The height of tree is , and all tree heights are pairwise distinct.
Pak Dengklek is training an orangutan so that she can jump from one tree to another. In one jump, the orangutan can jump left or right from the current tree to the first tree that is taller than the current one. Formally, if the orangutan is currently on tree , then she can jump to tree if and only if one of the following holds:
- is the largest non-negative integer less than among all such that ; or:
- is the smallest non-negative integer greater than among all such that .
Pak Dengklek has jumping plans. Each plan is described by four integers , , , and (). For each plan, Pak Dengklek wants to know whether the orangutan can start from some tree (), perform some number of jumps, and reach some tree (). If the plan is feasible, Pak Dengklek also wants to know the minimum number of jumps needed among all feasible ways.
You need to implement the following functions:
void init(int N, int[] H)
- : the number of trees.
- : an array of size , where is the height of tree .
- This function will be called exactly once before the first call to
minimum_jumps.
int minimum_jumps(int A, int B, int C, int D)
- : the range of tree indices that can be used as the starting point.
- : the range of tree indices that can be used as the ending point.
- This function should return the minimum number of jumps among all feasible ways, or return if the plan is not feasible.
- This function will be called exactly times.
Input Format
The sample grader program reads the input in the following format:
- Line : .
- Line : .
- Line (): , representing the parameters of the -th call to
minimum_jumps.
Output Format
The sample grader program outputs your answers in the following format:
- Line (): the return value of the -th call to
minimum_jumps.
7 3
3 2 1 6 4 5 7
4 4 6 6
1 3 5 6
0 1 2 2
2
1
-1
Hint
Sample Explanation
Consider the following call:
init(7, [3, 2, 1, 6, 4, 5, 7])
After initialization, consider the following call:
minimum_jumps(4, 4, 6, 6)
This plan means the orangutan must start from tree (height ) and reach tree (height ).
One feasible way with the minimum number of jumps is: first jump to tree (height ), then jump to tree .
Another way is: first jump to tree (height ), then jump to tree .
Therefore, minimum_jumps should return .
Consider another call:
minimum_jumps(1, 3, 5, 6)
This plan means the orangutan must start from one of tree (height ), tree (height ), or tree (height ), and finally reach either tree (height ) or tree (height ).
The only feasible way with the minimum number of jumps is: start from tree and jump directly to tree .
Therefore, minimum_jumps should return .
Consider another call:
minimum jumps(0, 1, 2, 2)
This plan means the orangutan must start from either tree (height ) or tree (height ), and finally reach tree (height ).
Since tree has the smallest height, it is impossible to jump to tree from any other tree.
Therefore, minimum_jumps should return .
Constraints
- .
- .
- ().
- ().
- .
Subtasks
- (4 points): ().
- (8 points): .
- (13 points): .
- (12 points): .
- (23 points): , .
- (21 points): .
- (19 points): no additional constraints.
Translated by ChatGPT 5
京公网安备 11011102002149号