Packets
Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & %llu
Submit Status Practice POJ 1017
Description
A factory produces products packed in square packets of the same height h and of the sizes 1*1, 2*2, 3*3, 4*4, 5*5, 6*6. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 6*6. 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 would save a lot of 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 1*1 to the biggest size 6*6. 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
题意:
有六种高都为h的不同底面积大小(底面分别为1、2、3、4、5、6边长的正方形)的产品各若干个,现在将它们用底面为6x6,高为h的盒子全部装起来,问最少需要多少盒子。
输入:
若干组输入数据,每组数据6个整数一排,从左至右依次表示底面边长为1、2、3、4、5、6的产品的个数。
输出:
每组数据,输出最少的盒子数。
分析:
使用贪心的思想。由于高全是相同的,我们只需要考虑底面的情况。我们从大至小依次选完每一种尺寸的产品。首先,6x6的产品每个皆占用一个盒子。然后,5x5的产品,每个都需要新增一个盒子。5x5的产品可以与11个1x1的产品共同占用一个盒子。4x4的产品,每个都需要新增加一个盒子。4x4的盒子既可以与5个及以下的2x2个产品共占一盒,如果此时盒子空间还有剩余,则用1x1的产品尽量填满。3x3的产品,4个可以刚好占满一个盒子,如果3x3产品的个数不被4整除,那么剩余的空间应先用2x2的产品尽量去填充,然后再用1x1的产品去填充。此时,我们已经使用完了3x3的产品了。2x2的产品9个可以刚好填满一个盒子,如果空间有剩余则用1x1的产品去填充。
1 #include<iostream> 2 using namespace std; 3 int main(void){ 4 int s1,s2,s3,s4,s5,s6;//6种size的盒子数量 5 while(cin >> s1 >> s2 >> s3 >> s4 >> s5 >> s6 && (s1 + s2 + s3 + s4 + s5 + s6)){ 6 int BoxNum = 0; //放进所有盒子所需的最少箱子数 7 BoxNum += s6; //6*6的盒子,每个都刚好独占一个箱子 8 BoxNum += s5; //5*5的盒子,放进箱子后,每个箱子余下的空间只能放11个1*1的盒子 9 s1 = max(0,s1 - s5 * 11); //把1*1的盒子尽可能地放进已放有一个5*5盒子的箱子 10 BoxNum += s4; //4*4的盒子,放进箱子后,每个箱子余下的空间为5个2*2的盒子空间 11 //先把所有2*2的盒子尽可能地放进这些空间 12 if(s2 >= s4 * 5) s2 -= s4 * 5; //若2*2的盒子数比空间多 13 //则消去已放进空间的部分 14 else{ //若2*2的盒子数比空间少则先把所有2*2的盒子放进这些空间 15 s1 = max(0,s1 - 4 * (s4 * 5 - s2)); //再用1*1的盒子填充本应放2*2盒子的空间 16 s2 = 0; //一个2*2空间可放4个1*1盒子 17 } 18 BoxNum += (s3 + 3) / 4; //每4个3*3的盒子完全独占一个箱子 19 s3 %= 4; //3*3的盒子不足4个时,都放入一个箱子,剩余空间先放2*2,再放1*1 20 if(s3){//当箱子放了i个3*3盒子,剩下的空间最多放j个2*2盒子 21 if(s2 >= 7 - 2 * s3){ //其中i={1,2,3} ; j={5,3,1} 由此可得到条件的关系式 22 s2 -= 7 - 2 * s3; 23 s1 = max(0,s1 - (8 - s3)); //当箱子放了i个3*3盒子,并尽可能多地放了个2*2盒子后 24 } //剩下的空间最多放j个1*1盒子,其中i={1,2,3} ; j={7,6,5} 25 else{ //但当2*2的盒子数不足时,尽可能把1*1盒子放入剩余空间//一个箱子最多放36个1*1,一个3*3盒子空间最多放9个1*1,一个2*2盒子空间最多放4个1*1 26 s1 = max(0,s1 - (36 - 9 * s3 - 4 * s2)); //由此很容易推出剩余空间能放多少个1*1 27 s2 = 0; 28 } 29 } 30 31 BoxNum += (s2 + 8) / 9;//每9个2*2的盒子完全独占一个箱子 32 s2 %= 9; //2*2的盒子不足9个时,都放入一个箱子,剩余空间全放1*1 33 if(s2) s1 = max(0,s1 - (36 - 4 * s2)); 34 BoxNum += (s1 + 35) / 36; //每36个1*1的盒子完全独占一个箱子 35 cout << BoxNum << endl; 36 } 37 return 0; 38 }