Steed 2: Cruise Control (Small)
InterviewTime limit5sMemory limit512 MB
Given horses ahead on a one-way road that slow to match slower horses they catch, find the fastest constant speed Annie can hold to her destination without ever passing one.
- Level
Medium4 of 10
- Topics
- Math, Implementation, Sorting, Greedy
- Solved
- No attempts yet
Problem
Annie is a bus driver with a high stress job. She tried to unwind on a Caribbean cruise, but that turned out to be stressful too, so she recently took up horseback riding.
Today Annie rides east along a long, narrow, one way road that runs from west to east. She is at kilometer 0 of the road and her destination is at kilometer . Kilometer markers increase from west to east.
other horses travel east on the same road. All of them keep going forever, and all of them are currently between Annie's horse and her destination. The -th of these horses starts at kilometer and travels at its maximum speed of kilometers per hour.
Horses are very polite, so a horse never passes a horse that started ahead of . Two or more horses may share the same position for any amount of time, and you may treat every horse as a single point. Every horse other than Annie's travels at its maximum speed, except that when catches up to a slower horse , slows down to match the speed of .
Annie's horse has no maximum speed and can travel at any speed Annie chooses, as long as it does not pass another horse. For a smooth ride, Annie wants to pick a single constant cruise control speed for the whole trip, from her current position to the destination, so that her horse never passes another horse. Find the maximum such speed.
Input
The first line contains the number of test cases, . test cases follow. Each test case begins with two integers and : the destination position of every horse in kilometers, and the number of other horses on the road. Then lines follow. The -th of those lines holds two integers and : the starting position in kilometers and the maximum speed in kilometers per hour of the -th of the other horses.
Limits
- for every
- for every , so no two horses start at the same position
Output
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 maximum constant speed in kilometers per hour that Annie can use without passing another horse. Print y with exactly six digits after the decimal point, rounding half up at the seventh digit.
Notes
In case 1 of the sample input there is one other horse and it is very slow: it reaches Annie's destination after 25 hours. Any speed above 101 kilometers per hour would make Annie pass that horse before she reaches the destination.
In case 2 there are two other horses. The faster horse catches the slower one at kilometer 240 after 2 hours. The two horses then travel at the slower horse's speed for 1 more hour and reach the destination at kilometer 300. The maximum speed Annie can pick without passing a horse is 100 kilometers per hour.