ACSL Practice 2009
On a straight single-lane road, there are cars parked with a space of meters wide separating every two adjacent cars. The cars will start to move successively in the same direction as follows.
At the beginning, the first car moves at a speed of meters per second, while the other cars are standing still. After every seconds from the beginning, every car checks the distance to the car in front or behind, and adapts its speed, if necessary.
The first car adapts as follows. If there are less than meters space to the car behind it, it immediately increases its speed by meters per second; if there are more than meters space to the car behind it, it immediately decreases its speed by meters per second; and otherwise it keeps its speed.
All the other cars adapt as follows. If there are less than meters space to the car in front, it immediately decreases its speed by meters per second; if there are more than meters space to the car in front, it immediately increases its speed by meters per second; and otherwise it keeps its speed.
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 or less.
The goal is to find out if a collision has occurred after -second-intervals, and if not, how far the first car will have traveled.
Note that the length of the cars is irrelevant.
Example 1. There are cars. The first car starts with meters per second, there are meters between adjacent cars and there are -second-intervals.
- 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 meters after the first three periods.
Example 2. There are cars. The first car starts with
meters per second, there are meters between adjacent cars
and there are -second-intervals.
Answer: A collision occurs.
Example 3. There are cars. The first car starts with
meters per second, there are meters between adjacent cars and
there are -second-intervals.
Answer: The first car will have traveled meters.
Input Specification
The input consists of a line containing the four integers , , , and in this order, and .
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