BouncingBall
SRM 783 · 2020-03-30 · by misof
Problem Statement
We are on a planet with no atmosphere. The gravitational acceleration on the surface of this planet is g cm/s^2 = (g/100) m/s^2.
We have just dropped a bouncy ball from the height of h meters. Each time the ball reaches the ground, it bounces upwards again. The bounce happens in an instant. In each bounce the ball loses p percent of its current kinetic energy.
Calculate and return the height of the ball (in meters) after t seconds.
Notes
- We assume that the gravitational acceleration is constant (i.e., it does not depend on the current height of the ball) and that its direction is straight down.
- Your return value must have an absolute or a relative error at most 1e-9.
- The constraints are such that after t seconds the ball is still bouncing. See Example #5.
Constraints
- g will be between 10 and 1000, inclusive.
- h will be between 10 and 1000, inclusive.
- p will be between 0 and 10, inclusive.
- t will be between 1 and 50, inclusive.
981 10 0 1 Returns: 5.095
We are on a planet whose gravitational acceleration constant is similar to our Earth: 9.81 m/s^2. We dropped an ideal bouncing ball from the height of 10 meters. After one second the ball is still falling: it is now 5.095 meters above ground and its speed is 9.81 m/s.
981 10 0 2 Returns: 6.408564143658009
The same situation as above, but now we want the situation after two seconds. Approximately 1.427843 seconds after the process started the ball hits the ground for the first time and bounces upwards again. As no energy got lost, the ball's speed immediately after the bounce is the same as its speed immediately before the bounce, only the direction changes from downwards to upwards. This speed is approximately 14.007141 m/s. The ball then travels upwards for approximately another 0.572157 seconds. After those seconds the ball is now approximately 6.408564 meters up in the air and its current speed upwards is 8.394282 m/s.
981 10 0 47 Returns: 1.5951180154118365
Still the same situation as above, now we want the height after 47 seconds. The ball keeps bouncing. Once every approximately 2.855686 seconds the ball hits the ground. During each bounce the maximum height the ball reaches is exactly h, and it happens exactly in the middle of the bounce. The last bounce before 47 seconds elapse will happen at roughly 44.263137 seconds. The first bounce after 47 seconds will happen at roughly 47.118823 seconds. Thus, at 47 seconds the ball is already on its way down and pretty close to hitting the ground again.
981 10 8 2 Returns: 6.081311391188509
Still the same planet, but now we used a less bouncy ball. This one loses 8% of its energy each time it bounces. This means that the maximum height of each bounce will be 8% lower than the maximum height of the previous bounce. The first 1.427843 seconds happen just like in all the examples above. However, once the ball bounces off the ground for the first time, its speed upwards will now be only 13.435178 m/s (instead of the 14.007141 m/s we saw in Example #1) and therefore the height of the ball after two seconds is slightly lower than in Example #1. (Please note that the speed immediately after the bounce is not 8% lower than the speed immediately before the bounce. The ball loses 8 percent of its energy, not speed.)
981 10 8 47 Returns: 0.7952075484286876
A general case: the same scenario as Example #3 but many bounces later.
1000 10 10 50 Returns: 0.036079462357220954
The test case with the most bounces possible. Note that the constraints imply that after t seconds the ball is still bouncing.
Submissions are judged against all 59 archived test cases, of which 6 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class BouncingBall with a public method double getPosition(int g, int h, int p, int t) · 59 test cases · 2 s / 256 MB per case