Statistics

Problem Statement for "WaterAndOxygen"

Problem Statement

You are in a spaceship that is far away from the Earth. Your crew needs costH2O moles of water (H2O) and costO2 moles of oxygen (O2) per day. The spaceship currently has remainH2O moles of water and remainO2 moles of oxygen left in the tanks. This ship is old so you don't have any equipment to recycle used water and oxygen: once you use them, they are gone. The only thing you can do is electrolysis of water. For any positive real x, you can electrolyze x moles of water. You will lose those x moles of water but you will produce x/2 moles of oxygen. (You will also produce x moles of hydrogen but those don't matter in this task.)

Compute and return the longest amount of time (in days) your crew can survive. Note that the answer is not necessarily an integer.

Definition

Class:
WaterAndOxygen
Method:
maxDays
Parameters:
int, int, int, int
Returns:
double
Method signature:
double maxDays(int remainH20, int remainO2, int costH2O, int costO2)
(be sure your method is public)

Notes

  • The returned value must have an absolute or relative error less than 1e-9.

Constraints

  • costH2O will be between 1 and 1,000,000,000, inclusive.
  • costO2 will be between 1 and 1,000,000,000, inclusive.
  • remainH2O will be between 0 and 1,000,000,000, inclusive.
  • remainO2 will be between 0 and 1,000,000,000, inclusive.

Examples

  1. 64

    70

    3

    7

    Returns: 12.0

    If you don't do anything, after 70 / 7 = 10 days you will run out of oxygen. At that time you will have 64 - 10 * 3 = 34 moles of water left. In order to survive longer you will need to electrolyze some of your remaining water to produce the oxygen you need. To get enough oxygen for a day, you need to electrolyze 2 * 7 = 14 moles of water. Don't forget that you also need 3 moles of actual water per day. Thus, on each of the following days you will consume 17 moles of water, which means that you can survive for 34 / 2 = 2 extra days. In total you will survive for 10 + 2 = 12 days.

  2. 99

    102

    1

    1

    Returns: 99.0

    You will run out of water after 99/1 = 99 days, while the oxygen will last for 102/1 = 102 days. Electrolysis won't help you live longer than 99 days.

  3. 101

    99

    1

    1

    Returns: 99.66666666666667

    This time you should use 4/3 of a mole of water to produce 2/3 of a mole of oxygen.

  4. 123456789

    987654321

    123

    456

    Returns: 1003713.731707317

  5. 987654321

    123456789

    456

    123

    Returns: 1758643.7307692308

  6. 0

    0

    13

    27

    Returns: 0.0

  7. 4

    8083

    6948

    20428

    Returns: 5.757052389176742E-4

  8. 424066370

    754

    41283343

    939090

    Returns: 9.825137032351709

  9. 2

    5044

    6479795

    502298

    Returns: 3.086517397541126E-7

  10. 134

    2790

    8652

    146

    Returns: 0.015487748497457236

  11. 37

    41727058

    40

    1

    Returns: 0.925

  12. 49

    23150346

    122300950

    9181

    Returns: 4.006510170199005E-7

  13. 8328781

    2003563

    19836081

    70741

    Returns: 0.41988036850625887

  14. 488973

    1

    2

    4783

    Returns: 51.1052466555184

  15. 2637835

    149

    644

    5403508

    Returns: 0.24409844499179287

  16. 1555

    9

    79

    62487

    Returns: 0.012578666645342376

  17. 676062

    167

    631

    22

    Returns: 1002.0681481481481

  18. 7335

    9985200

    32

    882471533

    Returns: 0.011319194948912738

  19. 4918

    65934125

    379905496

    233468

    Returns: 1.294532469727682E-5

  20. 6

    99193

    981935554

    74263

    Returns: 6.110380641131159E-9

  21. 25813

    524371083

    77

    2935

    Returns: 335.23376623376623

  22. 360771031

    13

    6581

    214413

    Returns: 828.5835023323007

  23. 31

    7742

    243899

    2193

    Returns: 1.271017921352691E-4

  24. 1

    213584

    632214

    9445

    Returns: 1.581742890856577E-6

  25. 682481

    333701880

    1

    823632

    Returns: 405.5730201273019

  26. 7035

    5

    9

    113523

    Returns: 0.031027724560128603

  27. 3520

    6266017

    6991

    767055061

    Returns: 0.008171184516341419

  28. 76

    77

    40560

    3697

    Returns: 0.001873767258382643

  29. 41

    7266

    4230

    945862204

    Returns: 7.703536177052895E-6

  30. 54

    976214

    956622

    77

    Returns: 5.644862861192822E-5

  31. 85

    8363754

    32099661

    52458

    Returns: 2.648003042773567E-6

  32. 8840573

    11632784

    17325

    855350

    Returns: 18.57967390518077

  33. 604332

    2491182

    113

    7328

    Returns: 378.27178549664836

  34. 17783260

    93

    322892289

    5722211

    Returns: 0.05319022833840104

  35. 2705037

    360437

    36

    50596

    Returns: 33.843511676611215

  36. 310987

    62019

    243416

    552

    Returns: 1.277594734939363

  37. 124

    89349

    57

    10788671

    Returns: 0.00828746782686829

  38. 138991

    85527

    12219

    770939176

    Returns: 2.0108106621270725E-4

  39. 919817

    77

    86820

    95127409

    Returns: 0.0048332619686713004

  40. 80759

    7261

    10

    319090

    Returns: 0.1492987981635563

  41. 7262

    591

    58

    14

    Returns: 98.18604651162791

  42. 6

    9565838

    788

    39119261

    Returns: 0.007614213197969543

  43. 8409011

    546395

    600089221

    13507005

    Returns: 0.014012934586605415

  44. 44540639

    34

    801189855

    75642

    Returns: 0.05558270358561985

  45. 5072145

    42082621

    99

    440167

    Returns: 101.35624970895003

  46. 5725718

    4

    262737

    371737348

    Returns: 0.007698585207556711

  47. 547748373

    84728

    7

    2634413

    Returns: 103.99225578035971

  48. 87

    4

    3

    309038

    Returns: 1.5370203485314985E-4

  49. 417891358

    6091764

    83

    690

    Returns: 293967.79630895425

  50. 2078019

    989203051

    8080

    33349

    Returns: 257.1805693069307

  51. 9144

    2725

    9597751

    8884

    Returns: 9.527231952568888E-4

  52. 373066

    1612

    992539063

    665830

    Returns: 3.758703449639443E-4

  53. 69258

    764308

    70

    785

    Returns: 974.3134146341464

  54. 64

    487218

    4256

    822

    Returns: 0.015037593984962405

  55. 9000

    17863020

    161229440

    9

    Returns: 5.582107089127147E-5

  56. 42802107

    31656519

    5

    4362623

    Returns: 12.161844398516443

  57. 1000000000

    1000000000

    1

    1

    Returns: 1.0E9

  58. 1000000000

    100

    1000000000

    1000000000

    Returns: 0.3333334

  59. 1

    1

    2

    2

    Returns: 0.5

  60. 2

    4

    3

    2

    Returns: 0.6666666666666666

  61. 6

    7

    2

    2

    Returns: 3.0

  62. 1000000000

    100000000

    1000000000

    1000000000

    Returns: 0.4

  63. 1000000000

    500000000

    1000000000

    1000000000

    Returns: 0.6666666666666666

  64. 2

    1

    1024

    1024

    Returns: 0.0013020833333333333

  65. 1

    1

    1

    1

    Returns: 1.0


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: