The Slowest Speed Past the Traps
Time limit2sMemory limit512 MB
Find the minimum constant running speed that lets Fred cross every trap during an inactive window, or report IMPOSSIBLE.
- Level
Medium7 of 10
- Topics
- Binary search, Math, Implementation
- Solved
- No attempts yet
Problem
Fred is escaping from an underground prison. He has already reached the outer corridor, and all that is left is a straight run to the exit door. The trouble is that the corridor is lined with traps. A trap works like a magnet, so Fred is held in place if he is inside the trap area while the trap is active. Standing exactly on the edge of a trap is safe.
Capturing a runner costs a trap a lot of energy, so a trap stays on only briefly. Each trap is active for seconds, then inactive for seconds, and repeats that cycle forever. Fred starts at position 0 at time 0, and every trap begins its cycle in the active state at that same moment.
Fred runs at one constant speed . If a trap covers the segment , Fred touches its near edge at time and leaves its far edge at time . He passes that trap safely only if some nonnegative integer satisfies
so that he both enters and leaves during a single inactive window of that trap.
Find the slowest speed that carries Fred safely past every trap.
Input
The first line has the number of test cases . ()
The first line of each test case has the number of traps . ()
Each of the next lines describes one trap as .
- is how long the trap stays active, in seconds. ()
- is how long the trap stays inactive, in seconds. ()
- is where the trap starts, in meters from the starting point.
- is where the trap ends, in meters from the starting point. ()
All values are integers. Segments of different traps may overlap.
Output
Print one line for each test case.
If a safe speed exists, print the slowest one in meters per second with four digits after the decimal point. Round at the fifth digit, and round up when that digit is exactly 5. If no such speed exists, print IMPOSSIBLE.
Hint
Take a corridor with a single trap where , , and .
The trap is inactive for only 1 second, so Fred has to cover the stretch from 2 meters to 3 meters within 1 second, which means his speed cannot be below 1 meter per second. The trap first turns off at second 3, so reaching the 2 meter mark before second 3 gets him caught, which means his speed cannot be above meter per second. No speed satisfies both conditions, so the answer is IMPOSSIBLE.