Snails
Time limit1sMemory limit128 MB
Each of N snails moves in a fixed direction at speed 1 and stops at the fence, at any point an earlier snail crossed, or when it meets another snail simultaneously; find when the last snail stops.
- Level
Hard8 of 10
- Topics
- Geometry, Simulation, Sorting, Implementation
- Solved
- No attempts yet
Problem
There are N snails inside a square fence. All snails start moving from their initial positions at the same time and follow these rules:
- Every snail's starting position is distinct.
- Each snail is given a single direction and moves only in a straight line along that direction.
- Every snail moves at the same speed of 1 cm per second.
- A snail stops when it reaches the fence (the boundary of the square).
- A snail stops as soon as it reaches a point that another snail has already passed through.
- If two or more snails reach the same point at the same time, all of them stop.
In other words, while a snail travels along its path it stops when it first (a) reaches the fence, (b) reaches a point that another snail passed through earlier than it, or (c) meets another snail at the same point at exactly the same time.
Given the starting position and direction of N snails that all start at the same instant, find the time it takes until the last snail stops. Round the answer at the third decimal place.
Input
The first line contains the number of snails () and a natural number (), the length in cm of one side of the fence, separated by a space. The bottom-left corner of the fence is at and the top-right corner is at .
Each of the next lines contains four integers () describing one snail: the snail starts at and moves in a straight line in the direction from toward . The points and are distinct.
Output
Print, on the first line, the time until all snails have stopped, rounded at the third decimal place. The time must always be shown to exactly two decimal places. For example, a time of 10.667 is printed as 10.67, a time of 5 as 5.00, and a time of 5.5 as 5.50.