Leap Frog
Time limit1sMemory limit128 MB
Given sorted positions, Jack and Jill alternate hopping over each other within distance 10; find the minimum total jumps until one lands on the last position.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Greedy, Two pointers
- Solved
- No attempts yet
Problem
Jack and Jill play a game called “Leap Frog” in which they take alternating turns jumping over each other. In a single jump, each of them can move a maximum horizontal distance of units.
You are given a list of valid positions on which a player may stand. Jill starts at position , Jack starts at position , and their goal is to reach position .
On every turn the player who is behind must hop over the player who is in front, landing on some valid position that lies strictly ahead of the front player. A jump is allowed only if the horizontal distance from the jumping player's current position to its landing position is at most . The two players may never occupy the same position at the same time.
Determine the minimum total number of jumps, counting the jumps of both players, needed until either Jack or Jill reaches position .
Input
The input contains multiple test cases. Each test case begins with a line containing a single integer (). The next line contains the integers where . The end of the input is marked by a line containing a single .
Output
For each test case, print on its own line the minimum total number of jumps needed for either Jack or Jill to reach position , or if neither of them can reach it.