Catch quails that flee in both directions on a line at given speeds by choosing the left-right chase order with the smallest total time.
Medium7Dynamic programmingMathNo attempts yetTime limit5sMemory limit512 MBAll N of your pet quail have gotten loose. You stand at coordinate 0 on a line. Quail i starts at a nonzero integer coordinate Pi, in meters, and runs away from you at the constant integer speed of Si meters per second. A quail keeps running away even while you are chasing something else. You run at the constant integer speed of Y meters per second and can change direction instantly at any moment. Whenever you occupy the same point as a quail, that quail is caught, and catching it takes no extra time.
Passing a quail requires catching it first, so a quail that starts at a positive coordinate always runs to the right, and a quail that starts at a negative coordinate always runs to the left.
What is the minimum number of seconds needed to catch every quail?
The first line contains the number of test cases T. T test cases follow. The first line of each test case contains two space separated integers Y, your speed, and N, the number of quail. The next line contains the positions P1,P2,…,PN, and the line after that contains the speeds S1,S2,…,SN, each list space separated.
For each test case, print one line in the form Case #x: y, where x is the test case number starting from 1 and y is the minimum number of seconds needed to catch every quail.
Round y to six decimal places and always print six digits after the decimal point. When the discarded part is exactly one half, round up. Every exact answer in the input data is at least 10−9 away from a rounding boundary.
In test case 1 of the first example, you run to the left and catch all three quail at the same moment, 12 meters to the left of your starting point. That takes 3 seconds.
One optimal strategy for test case 2 is this. Run left and catch the second quail at coordinate −2 after one second, then turn around and run right for four more seconds to catch the first quail at coordinate 6.