Boats shuttle back and forth over fixed ranges and riders transfer only where two boats meet, so compute the earliest arrival at the far bank.
Medium7Shortest pathGraphMathNo attempts yetTime limit1sMemory limit256 MBTo finish a hard Super Mario level you decided to write an AI and let it play for you. As a first step you leave out the badguys and implement movement only. The movement in this problem is crossing a river.
The river has width W and occupies x=[0,W]. You cross it on boats. Boat i travels only inside the range [Li,Ri], and every boat stays at height y=0. Treat a boat as a point of zero size.
At t=0 Mario is at x=0 and every boat is at its own left endpoint. A boat moves back and forth between its endpoints at a speed of one unit per second, forever. So a boat with range [L,R] is at x=L at t=0, at x=R at t=R−L, at x=L again at t=2(R−L), and at x=R again at t=3(R−L).
Mario cannot jump yet, so he moves from one boat to another only at a moment when the two boats have the same x coordinate. Time is continuous, so a transfer is allowed even when that moment is not an integer, and a transfer takes no time. When several boats share the same x coordinate, Mario may move to any of them.
Mario starts by boarding a boat located at x=0, and he has crossed the river the moment the boat he rides touches x=W. Find the minimal time to go from x=0 to x=W, or determine that x=W is unreachable.
The first line contains the number of test cases T. (1≤T≤20)
Each test case is given as follows.
For each test case, print on one line the earliest time at which Mario reaches x=W. That time is always an integer when x=W is reachable. Print IMPOSSIBLE when it is not.
In the first test case of the first example input, the two boats have the same period and stay 1 apart at all times, so Mario cannot move from one to the other.
In the second test case of the same example input, Mario transfers to boat 3 at time 2 and to boat 2 at time 16, which puts him at x=10 at time 24.