← 返回 bytedance 的题目列表Minimum Jumps to Reach Home
类型:online_judge
Problem: Minimum Jumps to Reach Home
A bug starts at position 0 on a number line and wants to reach position x.
At each move, it can choose one of the following:
Jump forward by a positions;
Jump backward by b positions.
The restrictions are:
The bug cannot jump to a negative position;
The bug cannot jump to any position in forbidden;
The bug cannot jump backward twice in a row.
Return the minimum number of jumps needed to reach x. If it is impossible, return -1.
Input Format
The first line contains an integer m, the number of forbidden positions.
The second line contains m integers, the array forbidden. If m = 0, this line is empty.
The third line contains three integers a b x.
Output Format
Print one integer: the minimum number of jumps to reach x.
Constraints
0 <= m <= 1000
1 <= forbidden[i], a, b, x <= 2000
Elements in forbidden are unique
0 is not in forbidden
Example
Input
5
14 4 18 1 15
3 15 9
Output
3