Statistics

Problem Statement for "Bounce"

Problem Statement

You are at the top of a building that is 125 meters tall with a bouncy ball and a horizontal launcher. Across the street, your friend is waiting on top of a smaller building of a given height. You want to fire the ball with your launcher such that the ball will land on top of the building that your friend is standing on. The ball must bounce one or more times on the street before landing on the other building. The ball you happen to have is a prototype which is completely elastic. This means it does not lose any energy when it bounces, and every bounce is exactly the same height as the previous bounce, which is exactly the same height as the original launcher. Because you want to keep control of this ball, your goal is to fire the ball at the slowest horizontal speed and still have it reach the top of the other building.

Using simple laws of motion from physics, we can derive formulas for the X and Y position of the ball over time. The X position is given by the formula:

X = Vi*t

Here, Vi is the initial launch velocity of the ball, and t is the time elapsed since the launch.

After the ball bounces, the Y position is dictated by the formula:

Y = 50*b +g*b*b/2

In this formula, g is the acceleration constant for gravity, which is fixed at -10. b is the time since the most recent bounce occurred. Each time the ball bounces, b is reset to 0. If unobstructed, the ball bounces for the first time 5 seconds after being launched. If still unobstructed, each bounce will occur 10 seconds after the previous bounce. So with a very wide street, the bounces would be at 5 seconds, 15 seconds, 25 seconds, 35 seconds, etc. Note that this property, and the Y position in general, does not depend at all on the launch velocity.

Given an int height of the second building and an int distance of how wide the street is between the buildings, determine the lowest integral speed at which you can launch the ball such that it will bounce at least once, and reach the top of the other building. The ball must be at or over the top of the other building when it reaches it for the first time, otherwise it may go into a window, and you don't want to lose the only 100% elastic ball in existence! If there is no integral speed that will satisfy these requirements, return -1.

Definition

Class:
Bounce
Method:
howFast
Parameters:
int, int
Returns:
int
Method signature:
int howFast(int height, int distance)
(be sure your method is public)

Notes

  • Assume that as long as the ball is at the height of the building or higher when the other building is reached, it will land on the building. There is no risk of the ball bouncing completely over the other building.
  • Assume that the ball has a negligible radius, and that no other forces besides gravity and bouncing off of the ground affect the ball.

Constraints

  • height is between 1 and 124, inclusive.
  • distance is between 5 and 500, inclusive.
  • For all speeds up to and including the correct speed, the ball will not reach the second building within .000001 meters of the top of the building.

Examples

  1. 124

    10

    Returns: 1

    By the time the ball reaches the other building, it is at its full height of 125 meters again, so all building heights that are 10 meters away will always return 1.

  2. 124

    9

    Returns: -1

    Now that the building is 1 meter closer, the ball will not make it to the top of the building after the first bounce, and if we fire at 2 m/s, it will reach the building before bouncing.

  3. 124

    99

    Returns: 5

    Firing the ball at 5 m/s, the ball will bounce twice before landing on the second building. The first bounce occurs 25 meters away from the first building, the second bounce at 75 meters away. When the ball reaches the second building, its height is 124.8 meters.

  4. 95

    196

    Returns: 2

    It takes 10 bounces before the ball finally goes over the top of the building.

  5. 1

    5

    Returns: -1

    Firing the ball at 1 m/s, the ball will land in the street just as it hits the other building, so it cannot be fired such that it bounces at least once and lands on the other building.

  6. 124

    105

    Returns: -1

  7. 124

    409

    Returns: 40

  8. 1

    62

    Returns: 1

  9. 32

    31

    Returns: 1

  10. 13

    297

    Returns: 1

  11. 12

    316

    Returns: 1

  12. 54

    101

    Returns: 1

  13. 27

    382

    Returns: 1

  14. 105

    273

    Returns: 3

  15. 54

    103

    Returns: 1

  16. 65

    69

    Returns: 1

  17. 40

    419

    Returns: 1

  18. 5

    10

    Returns: 1

  19. 107

    388

    Returns: 3

  20. 80

    102

    Returns: 1

  21. 20

    111

    Returns: 1

  22. 14

    170

    Returns: 1

  23. 30

    92

    Returns: 1

  24. 23

    152

    Returns: 1

  25. 15

    287

    Returns: 1

  26. 107

    300

    Returns: 1

  27. 81

    307

    Returns: 3

  28. 120

    112

    Returns: 11

  29. 123

    338

    Returns: 17

  30. 124

    197

    Returns: 10

  31. 119

    412

    Returns: 14

  32. 124

    294

    Returns: 15

  33. 123

    173

    Returns: 17

  34. 122

    342

    Returns: 17

  35. 110

    226

    Returns: 11

  36. 121

    225

    Returns: 11

  37. 124

    284

    Returns: 14

  38. 121

    387

    Returns: 13

  39. 123

    472

    Returns: 16

  40. 122

    462

    Returns: 23

  41. 123

    464

    Returns: 23

  42. 115

    375

    Returns: 12

  43. 123

    113

    Returns: 11

  44. 113

    225

    Returns: 11

  45. 124

    276

    Returns: 14

  46. 123

    115

    Returns: 11

  47. 121

    293

    Returns: 10

  48. 124

    369

    Returns: 36

  49. 124

    371

    Returns: 36

  50. 124

    312

    Returns: 30

  51. 124

    308

    Returns: 30

  52. 124

    309

    Returns: 30

  53. 1

    5

    Returns: -1

  54. 1

    6

    Returns: 1


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: