Statistics

Problem Statement for "RaceApproximator"

Problem Statement

If a runner races a distance D in time T, and later races a distance 2D, that runner will likely take more than 2T time to finish it. An examination of how times change with distances for a given runner can lead to the following approximation for the time it will take that runner to finish a given distance. Given two races with distances D1 and D2 which a runner ran in times T1 and T2, respectively, the approximate time it will take a runner to run a distance D is given by: T1*e^(ln(T2/T1)*ln(D1/D)/ln(D1/D2)).

When you race it is nice to have a time in mind that you'd like to be able to finish your race in. You are somewhat new to running and have only run two races of different distances. You are running a third race soon, and you want to use this equation to give you an estimate of how fast you should run. Since your upcoming race is a distance that falls between your first and second races' distances, you know this approximation will probably be fairly accurate. Create a class RaceApproximator with a method timeToBeat that takes ints d1, t1, d2, t2, and raceDistance, and returns a String that is the time you should be able to run in your upcoming race. d1, t1, d2 and t2 represent your shorter race's distance, your time in that race, your longer race's distance, and your time in that race, respectively. raceDistance is the distance of your upcoming race. All distances are in meters and all times are in seconds. Your return value should be truncated to an integer value, and formatted as "h:mm:ss" (all quotes are for clarity only) with"h" being the number of hours, "mm" being the number of minutes, and "ss" being the number of seconds.

Definition

Class:
RaceApproximator
Method:
timeToBeat
Parameters:
int, int, int, int, int
Returns:
String
Method signature:
String timeToBeat(int d1, int t1, int d2, int t2, int raceDistance)
(be sure your method is public)

Notes

  • In C++ e^x can be done with exp(x), and the natural log, ln(x), can be done with log(x), both functions are in math.h.
  • In C# e^x can be done with Math.Exp(x), and the natural log, ln(x), can be done with Math.Log(x). The Math class is in the System namespace.
  • In Java e^x can be done with Math.exp(x), and the natural log, ln(x), can be done with Math.log(x).
  • In Visual Basic e^x can be done with Exp(x), and the natural log, ln(x), can be done with Log(x), both functions are in the System.Math namespace.

Constraints

  • d1, t1, d2, t2, and raceDistance will all be between 1 and 10000, inclusive.
  • d1 will be less than d2
  • t1 will be less than t2
  • raceDistance will be greater than d1 and less than d2
  • To make the approximation reliable, all speeds (distance/time) will be between 1 meter/second and 10 meters/second, inclusive.
  • To avoid rounding errors, the return will never be within 1e-9 of an integer value.

Examples

  1. 800

    118

    5000

    906

    1500

    Returns: "0:03:57"

    Suzy Favor Hamilton's times for 800 meters and 5000 meters indicate that she should run 1500 meters in 3:57, which, in fact, is her time for 1500 meters.

  2. 400

    65

    1600

    350

    800

    Returns: "0:02:30"

    You can run 400 meters in 65 seconds, and 1600 meters in 5 minutes and 50 seconds, so you can probably run 800 meters in about 2 minutes and 30 seconds.

  3. 1600

    299

    10000

    2360

    5000

    Returns: "0:18:00"

  4. 100

    17

    10000

    4500

    9000

    Returns: "1:06:00"

  5. 1

    1

    10000

    9999

    5000

    Returns: "1:23:19"

  6. 1576

    1382

    7591

    4085

    5475

    Returns: "0:54:21"

  7. 900

    186

    9953

    7887

    4848

    Returns: "0:42:49"

  8. 822

    812

    7909

    7867

    5289

    Returns: "1:27:34"

  9. 6030

    5200

    9367

    8053

    8322

    Returns: "1:59:20"

  10. 2116

    522

    4723

    1073

    4399

    Returns: "0:16:46"

  11. 3616

    2102

    8316

    3707

    5965

    Returns: "0:49:16"

  12. 1853

    1416

    1982

    1623

    1900

    Returns: "0:24:49"

  13. 1866

    1105

    6686

    5780

    3123

    Returns: "0:35:54"

  14. 3835

    2140

    5760

    4525

    3967

    Returns: "0:37:57"

  15. 6248

    5500

    9593

    6066

    8652

    Returns: "1:38:44"

  16. 2213

    2147

    7729

    3828

    5758

    Returns: "0:55:40"

  17. 2266

    1581

    4246

    4184

    3263

    Returns: "0:46:21"

  18. 156

    117

    3863

    2754

    1755

    Returns: "0:21:06"

  19. 6677

    2462

    9160

    5715

    8404

    Returns: "1:15:43"

  20. 6762

    2480

    8097

    7519

    8045

    Returns: "2:00:26"

  21. 1931

    1689

    8294

    6252

    3470

    Returns: "0:47:38"

  22. 1944

    822

    6563

    5797

    2119

    Returns: "0:15:43"

  23. 656

    616

    7348

    6303

    1553

    Returns: "0:23:31"

  24. 4393

    3099

    9654

    5036

    6925

    Returns: "1:08:23"

  25. 2839

    2117

    7426

    7140

    3806

    Returns: "0:51:06"

  26. 6259

    3297

    9183

    6650

    7796

    Returns: "1:22:07"

  27. 3792

    1691

    8397

    6070

    7368

    Returns: "1:21:59"

  28. 1781

    1376

    9913

    8999

    4466

    Returns: "1:02:41"

  29. 3278

    2020

    7923

    6401

    7850

    Returns: "1:45:24"

  30. 4700

    2061

    7499

    6518

    5176

    Returns: "0:43:34"

  31. 800

    118

    5000

    906

    1500

    Returns: "0:03:57"

  32. 100

    17

    10000

    4500

    9000

    Returns: "1:06:00"

  33. 1000

    299

    10000

    2360

    5000

    Returns: "0:21:07"

  34. 400

    65

    1600

    350

    800

    Returns: "0:02:30"

  35. 156

    117

    3863

    2754

    1755

    Returns: "0:21:06"

  36. 1600

    299

    10000

    2360

    5200

    Returns: "0:18:49"

  37. 1600

    299

    10000

    2360

    5000

    Returns: "0:18:00"


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: