Bumper to bumper traffic
InterviewTime limit4sMemory limit512 MB
Two cars 4.4 m long start stopped on a line and alternate between stopping and driving at 1 m/s at given times; decide whether they ever touch, giving the first contact second rounded up.
- Level
Medium6 of 10
- Topics
- Implementation, Simulation, Math, Two pointers
- Solved
- No attempts yet
Problem
Rush hour moves at a crawl. At any moment each vehicle is either stopped or driving at the very slow speed of 1 meter per second. Cars now carry a simple black box that records every change in a car's speed. In this problem speeds change instantaneously.
The road is the real line, measured in meters, so a car is identified by its position on that line. Every car is 4.4 meters long.
You are given the initial positions of two cars driving along the line in the positive direction, together with a transcript of their speed changes. Decide whether the two cars ever collided. A collision at this speed is only a bumper tap, but it can corrupt the black box readings that follow, so the part of a transcript after a collision may not make sense.
Input
There is a single test case. The first line has two integers and , the initial positions of the rears of the two cars in meters (). Either or holds. At time 0 both cars are stopped.
The second line begins with , the number of times the first car changed speed (), followed by integers giving those times in seconds (). At time the first car starts driving at 1 meter per second, at time it stops, at time it starts again, and so on.
The third line begins with (), followed by integers () that describe the times the second car starts and stops.
Output
If the two cars collide, print bumper tap at time S on one line, where is the time in seconds from time 0 to the first contact, rounded up to a whole second. If they never collide, print safe and sound on one line.