[貪心]uva311 Packets



 Packets 

A factory produces products packed in square packets of the same height h and of the sizes tex2html_wrap_inline27 , tex2html_wrap_inline29 ,tex2html_wrap_inline31 , tex2html_wrap_inline33 , tex2html_wrap_inline35 , tex2html_wrap_inline37 . These products are always delivered to customers in the square parcels of the same height h as the products have and of the size tex2html_wrap_inline37 . 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 tex2html_wrap_inline27 to the biggest size tex2html_wrap_inline37 . 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*1, 2*2, 3*3, 4*4, 5*5, 6*6等六種尺寸。這些產品總是用高度爲h,面積爲6*6的箱子打包後寄給客戶。因爲成本關係,當然希望將客戶所訂購的產品放在最少的箱子裏寄出。請你寫一個程式找出寄送這些產品最少需要多少個箱子,這可以使工廠節省下不少錢。

思路:很典型的貪心思路了,首先6*6的盒子只能單獨放箱子裏,5*5的盒子可以和11個1*1的盒子搭配。4*4的盒子優先和2*2的盒子搭配,如果2*2的盒子沒了再與1*1的盒子搭配,3*3的盒子優先與2*2盒子搭配,再與1*1的盒子搭配,這裏要分好幾種情況。2*2的盒子只能與1*1的盒子搭配。想了好久,一開始想錯了,代碼寫的也很爛。。

代碼如下:

#include<iostream>
#include<algorithm>

using namespace std;

int arry[10];

int main()
{
    while(cin>>arry[1]>>arry[2]>>arry[3]>>arry[4]>>arry[5]>>arry[6])
    {
        if(arry[1]==0&&arry[2]==0&&arry[3]==0&&arry[4]==0&&arry[5]==0&&arry[6]==0) break;
        int sum=arry[6];
        while(arry[5]>0) //大小爲5*5的只能與1*1的搭配
        {
            sum++;
            arry[5]--;
            if(arry[1]>=11) arry[1]=arry[1]-11;
            else if(arry[1]<11&&arry[1]>0) arry[1]=0;
        }
        while(arry[4]>0)
        {
            sum++;
            arry[4]--;
            if(arry[2]>0) //4*4只能與2*2,1*1搭配,當2*2的還有時優先與2*2的搭配
            {
                if(arry[2]>=5) arry[2]=arry[2]-5;
                else if(arry[2]<5)
                {
                    int remain=20-4*arry[2];
                    arry[2]=0;
                    if(arry[1]>0) //當2*2不足時與1*1搭配
                    {
                        if(arry[1]<remain) arry[1]=0;
                        else arry[1]=arry[1]-remain;
                    }
                }
            }
            else //當2*2不足時與1*1搭配
            {
                if(arry[1]>=20) arry[1]=arry[1]-20;
                else arry[1]=0;
            }
        }
        while(arry[3]>0) //3*3的搭配種類比較多而且較爲麻煩,要分別考慮能放下多少個3*3的盒子
        {
            sum++;
            int remain,num;
            if(arry[3]>=4) arry[3]=arry[3]-4,remain=0,num=4;
            else remain=36-arry[3]*9,num=arry[3],arry[3]=0;
            if(num==4) continue;
            else if(num==1)
            {
                if(arry[2]>=5) //先與2*2搭配
                {
                    arry[2]=arry[2]-5;
                    remain=remain-20;
                    if(remain<arry[1]) arry[1]=arry[1]-remain;//再與1*1搭配
                    else arry[1]=0;
                }
                else //與1*1搭配
                {
                    remain=remain-arry[2]*4;
                    arry[2]=0;
                    if(remain>=arry[1]) arry[1]=0;
                    else arry[1]=arry[1]-remain;
                }
            }
            else if(num==2)
            {
                if(arry[2]>=3)
                {
                    arry[2]=arry[2]-3;
                    remain=remain-12;
                    if(remain<arry[1]) arry[1]=arry[1]-remain;
                    else arry[1]=0;
                }
                else
                {
                    remain=remain-arry[2]*4;
                    arry[2]=0;
                    if(remain>=arry[1]) arry[1]=0;
                    else arry[1]=arry[1]-remain;
                }
            }
            else if(num==3)
            {
                if(arry[2]>=1)
                {
                    arry[2]=arry[2]-1;
                    remain=remain-4;
                    if(remain<arry[1]) arry[1]=arry[1]-remain;
                    else arry[1]=0;
                }
                else
                {
                    remain=remain-arry[2]*4;
                    arry[2]=0;
                    if(remain>=arry[1]) arry[1]=0;
                    else arry[1]=arry[1]-remain;
                }
            }
        }
        while(arry[2]>0) //2*2的盒子只能與1*1的盒子搭配。
        {
            sum++;
            if(arry[2]>=9) arry[2]=arry[2]-9;
            else
            {
                int remain=36-arry[2]*4;
                arry[2]=0;
                if(remain>arry[1]) arry[1]=0;
                else arry[1]=arry[1]-remain;
            }
        }
        while(arry[1]>0)
        {
            sum++;
            if(arry[1]>=36) arry[1]=arry[1]-36;
            else arry[1]=0;
        }
        cout<<sum<<endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章