Classic greedy. Actually, it's hard to think about greedy strategies, but when I show this question to my classmates, they come up and say they use search... It seems that nothing is always difficult to think about ~
[Description]
All products produced by a factory must be packed in a box with a positive prism. The height of all the boxes is h, but the size of the bottom is different. 1x1, 2x2, 3x3, 4x4, 5x5, 6x6. As shown in.
These boxes will be placed in a box with a height of H and A base size of 6x6. To reduce shipping costs, the factory wants to minimize the number of boxes. If there is a good algorithm that can minimize the number of boxes, this will save the factory a lot of money. Please write a program like this.
[Input format]
A row contains 6 positive integers ( <= 100000 ), Indicating the number of boxes with the base sizes of 1x1, 2x2, 3x3, 4x4, 5x5, and 6x6 respectively.
[Output format]
An integer indicates the number of boxes.
[Example input]
3 2 1 1 1 1
7 5 1 0 0 0
0 0 0 0 0 0
[Sample output]
4
1
0
[Note]
(A) indicates that the box is filled with 3 * 3 And 6 * 6 (B) is the case of the second line.
This question seems very troublesome, and there are many situations. It is difficult to implement it by searching. We are very likely to think of the greedy method, that is, to fill the box as much as possible, so that the minimum number of boxes is required.
Therefore, the greedy strategy for this question is as follows:
- Load all the 6*6 boxes into the box separately ~).
- Load all 5*5 boxes into the box separately, and put as many 1*1 boxes as possible in the remaining 11 1*1 spaces.
- Load all the 4*4 boxes into the box separately, and try to put 2*2 boxes in the remaining space. Fill the remaining box with 1*1.
- 3*3...
- After all the boxes are placed, the number of required boxes is the optimal solution.
(Thanks to hjy for providing the questions and ideas. For more information, see the source)