算法:装箱问题——bin-packing problem(greedy algorithm)

problem:

Afactory produces products packed in square packets of the same height h and ofthe sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered tocustomers in the square parcels of the same height h as the products have andof the size 6*6. Because of the expenses it is the interest of the factory aswell as of the customer to minimize the number of parcels necessary to deliverthe ordered products from the factory to the customer. A good program solvingthe problem of finding the minimal number of parcels necessary to deliver thegiven products according to an order would save a lot of money. You are askedto make such a program.

Sample Input

00 4 0 0 1

75 1 0 0 0

00 0 0 0 0

Sample Output

2

1

Understanding and solving ideas:

This problem is described more clearly,here only to explain the input and output samples: a total of two sets of validinput, the first group said there are 4 3X3 products and a 6X6 products, thistime 4 3X3 products occupy a box , Another 6X6 product occupies a box, so thenumber of boxes is 2; the second group said there are 7 1X1 products, 5 2X2products and a 3 × 3 products, you can put them all in a box In, so the outputis 1.

Analysis of 6 models of products occupy the box asfollows:

6X6 products each occupy a complete box,and there is no free space; 5X5 products each occupy a new box and leave 11free space for 1X1 products; 4X4 products each occupy A new box, and leave 5free space for 2X2 products; 3X3 products more complex, first 3X3 products cannot be placed in the original box 5X5 or 4X4, then you must open for the other3X3 New boxes, the number of new boxes equal to 3X3 is divided by 4, and thenumber of new boxes is rounded up. Also consider how much of the remainingspace can hold 2X2 1X1 and 3X3 products when opening a box (where space Canhold 2X2 products, it will be credited to 2X2 free space until 2X2 finished allthe products, if there are 2X2 space remaining, and then convert them into 1X1remaining space) points to discuss the situation for the 3X3 products Open thenew box in the amount of the remaining vacancies, a total of four kinds ofsituations: first, 3X3 the number of products is exactly a multiple of 4, thereis no free space; the second, 3X3 the number of products is a multiple of 4 1,there are still 5 2X2 vacancies and 7 1X1 vacancies; the third, 3X3 productnumber is multiples of 4 plus 2, then there are 3 2X2 vacancies and 6 1X1vacancies; 4, 3X3 product number is a multiple of 4 plus 3, then there is a 1X2vacancies and 5 1X1 vacancies; finished processing 3X3 products, you cancompare the remaining 2X2 vacancies and 2X2 products If the number of productsis large, fill up 2 × 2 slots, open a new box for a 2 × 2 product, andcalculate a 1x1 slot in a new box. If there are more remaining slots, set 2 × 2The product all fill in the 2 × 2 vacancies, and then the remaining 2 × 2vacancies into the 1x1 vacancies; the last processed 1x1 products, compare the1x1 vacancies and 1x1 the number of products, if more space, Fill all the 1 × 1products in the free space, otherwise, fill the 1 × 1 space first, and thenopen a new box for the 1x1 product.



#include
using namespace std;

int minPack(int p1,int p2,int p3,int p4,int p5,int p6)
{
		int sum=0;
		sum=p6+p5+p4;
		p1-=min(p1,p5*11);
		if(p2





你可能感兴趣的:(基础算法,C/C++)