TopCoder problem "WarehouseJob" used in TCHS SRM 18 (Division I Level Three)

Problem Statement


Recently you got a job and have been working in a warehouse. One of your tasks is to arrange a number of boxes one on top of another in a single pile, so that they take up less space on the floor. The boxes have different weights, as each of them holds different goods inside. Because some goods need to be accessed more often than others, certain boxes must lie above certain other boxes. Because you are very lazy, you decided to write a program that will calculate an arrangement that requires the least effort from your side.

Placing the bottom most box doesn't cost any effort. Placing each of the other boxes requires w * h effort, where w is the weight of the box, and h is the number of the boxes arranged so far. You can assume that all boxes are cubes with the same dimensions.

You are given a int[] weight, which represents the weights of the various boxes. You are also given a String[] above, where above[i][j] is '1' if the i-th box must lie above the j-th box, and '0' if there is no such constraint. Return a int[] containing the ordering of the boxes that requires the least total effort. Each element of the int[] is the 0-based index of a box, and the elements must be ordered from bottom to top. If there are multiple such orderings, return the one where the bottom box has the lowest index. If a tie still remains, return the one where the second box from the bottom has the lowest index, etc.



Parameters:int[], String[]
Method signature:int[] stackBoxes(int[] weight, String[] above)
(be sure your method is public)


-weight will contain between 2 and 16 elements, inclusive.
-Each element of weight will be between 1 and 1000, inclusive.
-weight and above will contain the same number of elements.
-Each element of above will contain exactly n characters, where n is the number of elements in weight.
-above will contain only the digits '0' and '1'.
-There will always be at least one possible ordering.


Returns: {2, 1, 0 }
We have boxes of weights 1, 2 and 3 and no contraints about their order. Therefore it's best to put the heaviest box on the ground, the second heaviest on top of it, and the lightest on the top.
Returns: {1, 2, 0 }
The last box must lie above the second box.
Returns: {0, 10, 3, 7, 8, 13, 6, 15, 5, 14, 4, 2, 1, 12, 9, 11 }

Problem url:

Problem stats url:




PabloGilberto , brett1479 , Olexiy

Problem categories:

Dynamic Programming