Statistics

Problem Statement for "BerryPacker"

Problem Statement

You are selling a number of boxes of berries to a distributor. You are paid per berry, but the distributor only knows how many boxes you are shipping - not how many berries are in each box. To get around this, the distributor employs inspectors to estimate the total number of berries. Each inspector will take a sample of the boxes, count the berries in each of the sample boxes, and use this to extrapolate the total number of berries to pay for. It is up to you to decide how many berries to put in each individual box. The total number of berries to package is given in berries, and all berries must be used. You can put between 1 and 9 berries, inclusive, in each box.

Each inspector n starts at the box numbered first[n] and then proceeds to box first[n]+period[n] and continues inspecting boxes until reaching the end of the boxes (boxes begin numbering at zero). If an inspector were to see a total of 30 berries in the 20 boxes looked at, and if there were 25 boxes altogether, that inspector would give an estimate of 30*25/20=37.5 berries for the entire shipment. If an inspector does not open any boxes, that inspector will estimate that there are zero berries in the shipment. More than one inspector may look at the same box. You will be paid based on the average estimate of the inspectors.

Since you know which boxes each inspector will look at, it is in your best interest to put more berries in those boxes and to choose a number of boxes that will make your shipment seem as large as possible. Assuming that you do so, what is the maximum number of berries you will be paid for?

Definition

Class:
BerryPacker
Method:
bestPacking
Parameters:
int[], int[], int
Returns:
double
Method signature:
double bestPacking(int[] first, int[] period, int berries)
(be sure your method is public)

Constraints

  • first and period will each contain between 1 and 5 elements, inclusive.
  • first and period will contain the same number of elements.
  • Each element of first will be between 0 and 100,000 inclusive.
  • Each element of period will be between 1 and 100,000 inclusive.
  • berries will be between 1 and 100,000 inclusive.

Examples

  1. {2}

    {500}

    6

    Returns: 12.0

    There's only one inspector, and he's only going to see one box - box 2 (you don't have nearly enough berries to think about box 502). One way to maximize your payment is to use 4 boxes, put 3 berries in box 2 and 1 berry in each of the other 3 boxes. Inspector sees 3 berries in one box and assumes there are 12 in shipment.

  2. {0,1}

    {2,2}

    7

    Returns: 9.0

    Between the two inspectors, each box is going to get looked at once (inspector 0 looks at even boxes, inspector 1 at odd boxes). Your best bet is to put 5 berries in box 1 and 1 berry in boxes 0 and 2. That way inspector 1 sees 5 berries and assumes there's 15. Inspector 0 sees 2 berries in 2 boxes, and estimates a shipment of 3. (15+3)/2=9

  3. {500}

    {1}

    499

    Returns: 0.0

    Inspectors that don't see anything will assume there's no berries.

  4. {0,1,2,3,4}

    {2,2,2,2,2}

    5

    Returns: 5.0

    We don't have any leeway to game system here.

  5. {1,2,3,4,549}

    {3,5,9,11,1}

    550

    Returns: 969.6

    It's not worth trying to show anything to last inspector.

  6. {1,2,3,4,5}

    {1,1,1,1,1}

    100000

    Returns: 100023.99604073784

    We can't game much here - all we can do is stiff the first few boxes a bit

  7. {0,0,0,0,0}

    {1,3,5,7,11}

    100000

    Returns: 197695.29425500863

  8. {1}

    {1}

    1000

    Returns: 1007.9999999999999

    1000 = (111*9) + 1 9*112/111=1008

  9. {2,5,9,25}

    {1,3,11,7}

    100

    Returns: 251.50649350649354

  10. {3892,4897,2872,8711,7191}

    {1,298,47872,2992,19871}

    100000

    Returns: 720091.816755946

  11. {0,0,0,0,0}

    {1,1,1,1,1}

    100000

    Returns: 100000.0

  12. {5,3,6782,2,80000}

    {5001,5002,5003,5004,1}

    100000

    Returns: 739511.9999999999

  13. {78,292,29922,4774,27171}

    {928,2817,88,1,992}

    500

    Returns: 1742.4

  14. {27,68,51,44,41}

    {18,94,73,70,76}

    92171

    Returns: 488756.91014572454

  15. {9293,18855,7046,9972,9138}

    {25028,12090,13554,22805,25799}

    91791

    Returns: 824247.0

  16. {39618,7642,30532,82562,15014}

    {3,3,2,2,2}

    91737

    Returns: 235544.93287874284

  17. {1200,901,4447,3051,2951}

    {1,1,1,1,1}

    90799

    Returns: 108747.10794387218

  18. {6,1,3,1,6}

    {1001,1000,1003,1002,9003}

    18001

    Returns: 156897.0

  19. {0,0,1,1,1000}

    {1,2,1,2,500}

    15000

    Returns: 38596.8030321489

  20. {0, 1, 2, 3, 4 }

    {1, 2, 3, 5, 7 }

    100000

    Returns: 171703.9315824347

  21. {1000 }

    {1 }

    100000

    Returns: 108000.0

  22. {1, 2, 3, 4, 5 }

    {2, 3, 5, 7, 11 }

    100000

    Returns: 197899.17731785311

  23. {1, 2, 3, 4, 5 }

    {1, 2, 3, 4, 5 }

    100000

    Returns: 166288.75076498027

  24. {1, 2, 3, 4, 55 }

    {101, 72, 202, 1024, 303 }

    100000

    Returns: 712512.0

  25. {0, 3, 3, 3, 3 }

    {100, 100, 100, 100, 100 }

    4

    Returns: 4.0

  26. {2, 5, 9, 25, 29 }

    {1, 3, 11, 7, 13 }

    100000

    Returns: 217813.74814447263

  27. {0, 0, 0, 0, 0 }

    {1, 1, 1, 1, 1 }

    100000

    Returns: 100000.0

  28. {0, 10, 20, 30, 40 }

    {99999, 99989, 99799, 99999, 99999 }

    100000

    Returns: 899568.0

  29. {1, 2, 3, 3, 4 }

    {2, 3, 4, 5, 6 }

    100000

    Returns: 161719.2685123893


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: