Starting from the best blue step on a circular escalator, pick jump heights that change by at most one to maximize the height reached before landing on red.
Medium7GraphBFSBrute forceNo attempts yetTime limit5sMemory limit512 MBRobbit Downey Hopper invented the dangerous sport of Extreme Escalator Pogo. It really is dangerous, so do not try it at home even if you own an escalator. Do not try it anywhere.
Extreme Escalator Pogo needs two things: a jumping device called a pogo stick, and a fast escalator with N steps that rise at a constant speed. Some of the steps are blue and the rest are red. Robbit starts by jumping onto a blue step in the middle of the escalator with his pogo stick. From then on he keeps bouncing straight up while the steps move underneath him. He may land only on blue steps. The moment he lands on a red step he is out, and his challenger Leepie Froggison gets her turn to jump. Whoever reaches the greatest height wins.
After touching his first blue step, Robbit's first jump always has height 1 and takes him to the very next step. (That step had better be blue.) On every jump except the first one he picks one of three options:
A jump of height H takes exactly as long as H escalator steps need to pass under Robbit, so a jump of height H from step i lands on the step that sits H positions after step i. A jump of height 0 is not allowed, and the height has no upper bound. The steps run on a loop that cycles forever.
The height of a jump still counts when that jump ends on a red step. Given N and the colour of every step, find the greatest height Robbit can reach when he picks the best starting step and the best sequence of jumps.
The first line gives the number of test cases T. Each of the next T lines contains an integer N, then an integer K, then K integers in the range 1 to N that list the blue steps. Every other step is red. The step after step N is step 1.
For each test case print one line in the form Case #x: H, where x is the test case number starting from 1 and H is the greatest height Robbit can reach. If the height has no upper bound, print infinity in place of H.
In the first case of the first example, N=4 and steps 2 and 3 are blue. Robbit starts on step 2, jumps with height 1 onto step 3, then amplifies to height 2 and lands two positions later on the red step 1. The greatest height reached this way is 2.
In the second case the best plan starts on step 5 and goes to step 6 (height 1), step 8 (height 2), step 1 (height 3), step 5 (height 4), and finally step 10 (height 5), which is red.
In the third case N=3 and steps 1 and 2 are blue. Robbit starts on step 1 and can keep jumping forever, so he reaches any height he wishes.