Horse Cruise Control
Time limit5sMemory limit512 MB
Find the largest constant speed Annie can ride without passing any horse that slows as it catches leaders, expressed as a reduced fraction.
- Level
Medium6 of 10
- Topics
- Math, Greedy, Sorting, Implementation
- Solved
- No attempts yet
Problem
Annie drives a bus and the job wears her down. She booked a Caribbean cruise to unwind, that turned out to be stressful too, so lately she rides horses instead.
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 marks grow 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. Horse starts at kilometer and runs at its maximum speed of kilometers per hour.
Horses are very polite. 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 each horse counts as a single point. Every horse other than Annie's runs at its maximum speed, except that once catches up to a slower horse ahead of it, drops to the speed of .
Annie's horse has no maximum speed, so it runs at any speed Annie chooses as long as it passes no other horse. For a smooth ride Annie wants a single constant speed for the whole trip, from her current position to the destination, and that speed must never make her pass another horse. Find the largest such speed.
Input
The first line contains the number of test cases . test cases follow.
The first line of each test case contains two integers and . is the destination position of every horse in kilometers, and is the number of other horses on the road. Each of the next lines contains 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 all
- for all (no two horses start at the same position)
Output
For each test case, print one line of the form Case #x: p/q, where x is the test case number starting from 1 and p/q is the largest constant speed in kilometers per hour that Annie can hold without passing another horse, written as a fraction in lowest terms.
The answer is always rational. Use integers and with and . When the answer is an integer , print the denominator anyway, as n/1.
Hint
The first test case of the example has one other horse, and it is very slow. It reaches Annie's destination after 25 hours. Anything faster than 101 kilometers per hour makes Annie pass that horse before she reaches the destination, so the answer is 101/1.
The second test case has two other horses. The faster one catches the slower one at kilometer 240 after 2 hours. Both then run at the slower horse's speed for 1 more hour and reach the destination at kilometer 300. The largest speed Annie can choose without passing another horse is 100 kilometers per hour, so the answer is 100/1.