[Small, greedy, and wise] package question solutions

Source: Internet
Author: User

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:

    1. Load all the 6*6 boxes into the box separately ~).
    2. 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.
    3. 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.
    4. 3*3...
    5. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.