Runaway Quail
Time limit5sMemory limit512 MB
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.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Math
- Solved
- No attempts yet
Problem
All of your pet quail have gotten loose. You stand at coordinate on a line. Quail starts at a nonzero integer coordinate , in meters, and runs away from you at the constant integer speed of meters per second. A quail keeps running away even while you are chasing something else. You run at the constant integer speed of 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?
Input
The first line contains the number of test cases . test cases follow. The first line of each test case contains two space separated integers , your speed, and , the number of quail. The next line contains the positions , and the line after that contains the speeds , each list space separated.
Limits
- ,
Output
For each test case, print one line in the form Case #x: y, where is the test case number starting from 1 and is the minimum number of seconds needed to catch every quail.
Round 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 away from a rounding boundary.
Hint
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 after one second, then turn around and run right for four more seconds to catch the first quail at coordinate .