Ultraviolet A 311-Packets

Source: Internet
Author: User

Original question:
A factory produces products packed in square packets of the same height h and of the sizes ,,,,,. these products are always delivered to mers MERs in the square parcels of the same height h as the products have and of the size. because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels necessary to deliver the given products according to an order wocould save a of lot money. you are asked to make such a program.
Input
The input file consists of several lines specifying orders. each line specifies one order. orders are described by six integers separated by one space representing successively the number of packets of individual size from the smallest size to the biggest size. the end of the input file is indicated by the line containing six zeros.
Output
The output file contains one line for each line in the input file. this line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. there is no line in the output file corresponding to the last ''null'' line of the input file.
Sample Input
0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0
Sample Output
2
1


Question:
Boxes of 1*5, 6*6 sizes must be packed in 6*6 boxes with the same height. Use the minimum 6*6 boxes to pack all sizes of the boxes.

Analysis and Summary:
This is my last question in Volume 4. Algorithm Design, mainly because I have no idea. It is best to refer to others' ideas.

6*6 boxes can be filled with boxes of various sizes. The following situations may occur:
1x6
1x5 + 11x1X1
1 4*4 + 5 2*2 (2*2 is preferred when there is a gap. If 2*2 is not completed, 1*1 is left for the rest)
When 3*3 is placed, the combination is complex. When 3*3 is not completed, the remaining gaps are prioritized as many as possible to put 2*2
When you place 1 3*3, you can also place 7 1*1 and 5 2*2
When you place 2 3*3, you can also place 6 1*1 and 3 2*2
When you place 3 3 3*3, you can also place 5 1*1 and 1 2*2

Because a 4*4, 5*5, 6*6 can only be placed in a box, how many boxes are needed for installation.
Then, because 3*3 can be combined with 1*1 and 2*2, you can determine the boxes required for 3*3.

In the calculation, we can first determine the number of boxes after 3*3, 4*4, 5*5, and 6*6. Assume It is a t box.
Then there will be gaps in the t box, and the 2*2 will be placed in the gaps in the t box first. If these gaps cannot be filled with 2*2, you need to add the box to know how to finish it. Finally, consider placing 1*1.


Code:
[Cpp]
/*
* Ultraviolet A: 311-Packets
* Time: 0.008 s
* Author: D_Doubel
*
*/
# Include <iostream>
# Include <cstdio>
Using namespace std;
Int arr [7];
 
// Set the combination of 3 in 6*6 to give priority to 2*2.
// When you place 1 3*3, you can also place 7 1*1 and 5 2*2
// When you place 2 3*3, you can also place 6 1*1 and 3 2*2
// When three 3*3 are placed, five 1*1 and one 2*2 can also be placed.
Int three [4] [2] = {0, 0}, {7, 5}, {6, 3}, {5, 1 }};
 
Int main (){
While (~ Scanf ("% d", & arr [1], & arr [2], & arr [3], & arr [4], & arr [5], & arr [6]) {
If (! Arr [1] &! Arr [2] &! Arr [3] &! Arr [4] &! Arr [5] &! Arr [6]) break;
 
Int ans = (arr [3] + 3)/4 + arr [4] + arr [5] + arr [6]; // 3*3, 4*4, 5*5, 6*6 I used several
 
Arr [1]-= arr [5] * 11; // a 5*5 and 11 1*1 can be placed for 6*6, therefore, place 1*1 and 5*5 in a box first.
 
Int left_for_2 = three [arr [3] % 4] [1] + arr [4] * 5; // The total number of 2x2 space available in the used box
 
If (arr [2] <= left_for_2) {// when the space in the used box can be placed in 2*2 boxes
Arr [1]-= three [arr [3] % 4] [0]; // number of 1*1 Numbers that can be placed in a combination of 2*2 in a gap
Arr [2]-= left_for_2;
Arr [1] + = arr [2] * 4; // If 2*2 is a negative number, the absolute value of the negative number is a gap and can be used to place 1*1
If (arr [1]> 0) // if 1*1 still exists, add the box
Ans + = (arr [1] + 35)/36;
}
Else {// cannot be placed 2*2
Arr [1]-= three [arr [3] % 4] [0]; // fill the box 1*1 together with 2*2
Arr [2]-= left_for_2;
Ans + = (arr [2] + 8)/9; // Add the box until the box can be filled with 2*2
Arr [1]-= (9-arr [2] % 9) * 4; // can the added box be filled with 2*2 and 1*1 space?
If (arr [1]> 0)
Ans + = (arr [1] + 35)/36;
}
Printf ("% d \ n", ans );
}
Return 0;
}


Author: shuangde800

Related Article

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.