ACSL Practice 2009
On a straight single-lane road, there are
At the beginning, the first car moves at a speed of
The first car adapts as follows. If there are less than
All the other cars adapt as follows. If there are less than
Against the laws of physics, any change of speed takes immediate effect.
If a car stands still and needs to decrease speed, it will remain
standing still; the cars never go backwards. A collision occurs if the
space between two cars becomes
The goal is to find out if a collision has occurred after
Note that the length of the cars is irrelevant.
Example 1. There are
- The first car will travel
meters in the first period, while all other cars stand still in this period. - At the beginning of the second period, the first car decelerates to
a speed of
meters per second, because there is now meters space to the car behind, and thus travels meters in the second period. At the beginning of the second period, the second car accelerates to a speed of meters per second, because there is meters space to the car in front, and thus travels meters in the second period. At the beginning of the second period, all other cars remain standing, because there still are meters space to the car in front. - At the beginning of the third period, the first car decelerates to a
speed of
meters per second, because there is again meters space to the car behind, and thus travels meters in the third period.
Answer: The first car will have traveled
Example 2. There are
Answer: A collision occurs.
Example 3. There are
Answer: The first car will have traveled
Input Specification
The input consists of a line containing the four integers
Output Specification
The output contains the number of meters traveled by the first car if
there is no collision. If there is a collision, output -1
.
Sample Input 1
5 10 80 3
Sample Output 1
75
Sample Input 2
2 60 100 14
Sample Output 2
-1
Sample Input 3
20 5 5 100
Sample Output 3
2550
Comments