#P1033. Catching Falling Balls
Catching Falling Balls
Catching Falling Balls
There are n balls attached to a ceiling at height \(H\) with horizontal positions \(0, 1, 2, \ldots, n-1\) (each considered a point). On the ground, a car is moving with constant speed \(V\). The car is a rectangle with length \(L\) and height \(K\), and its left edge is initially at horizontal distance \(S_1\) from the origin.
At time \(t=0\) the balls begin to fall under gravity and the car starts moving. The falling distance of a ball is given by the formula \[ d = 0.5\times g\times t^2, \quad g=10, \] so that at time \(t\) a ball’s vertical coordinate is \(H-0.5\times10\,t^2\). Meanwhile, at time \(t\) the car occupies the horizontal interval \[ [S_1+Vt,\; S_1+Vt+L]. \]
A ball is caught (i.e. accepted by the car) if, before it hits the ground, there exists a moment when the distance from the ball to the car is less than or equal to \(0.0001\). (Note that once a ball touches the ground it can no longer be caught.)
In this problem the only possible way for a ball to be caught is when it is directly above the car. To be precise, consider the moment when the ball’s vertical distance to the top of the car equals \(0.0001\). Since the car’s top is at height \(K\), this happens when \[ H-0.5\times10\,t^2 = K+0.0001. \] Let \[ t_{max}=\sqrt{\frac{2(H-K-0.0001)}{10}}. \] For a ball at horizontal position \(i\) (with \(0\le i0\), it must happen that at time \(t_{max}\) the car covers position \(i\); that is, \(i\) lies in the interval \[ [S_1+Vt_{max},\; S_1+Vt_{max}+L]. \] Rearranging, for \(V>0\) the ball at \(i\) is caught if and only if \(t_{max}\) satisfies \[ \max\Bigl(0, \frac{i-L-S_1}{V}\Bigr) \le t_{max} \le \frac{i-S_1}{V} \quad \text{(with the convention that if } i-S_1 < 0 \text{ no catch occurs)}. \] For the special case \(V=0\) the car is stationary and the ball is caught if and only if \(i\) is in the interval \([S_1,\; S_1+L]\) (since then at time \(t_{max}\) the ball will be exactly above the car).
Your task is to compute how many balls the car will catch.
inputFormat
The input consists of a single line containing six numbers: n H L K S1 V, where:
- n is an integer representing the number of balls,
- H is the height of the ceiling,
- L is the length of the car,
- K is the height of the car,
- S1 is the initial horizontal distance of the car from the origin,
- V is the speed of the car.
outputFormat
Output a single integer indicating the number of balls caught by the car.
sample
5 20 3 2 1 1
2
</p>