POJ 1017

Packets

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 61612   Accepted: 20889

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 

Source

Central Europe 1996

 

大致题意:

一个工厂制造的产品形状都是长方体盒子,它们的高度都是 h,长和宽都相等,一共有六个型号,分别为1*1, 2*2, 3*3, 4*4, 5*5, 6*6。

这些产品通常使用一个 6*6*h 的长方体箱子包装然后邮寄给客户。因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的箱子数量BoxNum。

解题思路:

由于盒子和箱子的高均为h,因此只需考虑底面积的空间。

6*6的盒子,每个盒子独占一个箱子。

5*5的盒子,每个盒子放入一个箱子,该箱子的剩余空间允许放入的最大尺寸为1*1,且最多放11个。

4*4的盒子,每个盒子放入一个箱子,该箱子的剩余空间允许放入的最大尺寸为2*2,且最多放5个,剩下的用1*1填充。

3*3的盒子,每4个刚好独占一个箱子,不足4个3*3的,剩下空间由2*2和1*1填充。

                   不足的情况分为三种,剩下1个3*3时,最多还能放5个2*2,剩下的用1*1填充。

                                                       剩下2个3*3时,最多还能放3个2*2,剩下的用1*1填充。

                                                       剩下3个3*3时,最多还能放1个2*2,剩下的用1*1填充。  

2*2的盒子和1*1的盒子主要用于填充其他箱子的剩余空间,填充后的多余部分才开辟新箱子装填。

代码:

我自己写得,理解起来比较简单,但是有点长= =

#include <iostream>
using namespace std;
//装入6*6 
int packet6(int a[6],int num)
{
    num+=a[5];
    return num;
    
}
//装入5*5 
int packet5(int a[6],int num)//6*6=5*5+11*1*1
{
    num+=a[4];
    int remain=a[4]*11;
    while(remain!=0&&a[0]!=0)//用1*1填充 
    {
        remain--;
        a[0]--;
    }
        return num;
}
//装入4*4 
int packet4(int a[6],int num)//6*6=4*4+5*2*2=4*4+20*1*1
{
    num+=a[3];
    int remain=20*a[3];//装了4*4之后剩下的格子数
    //装入2*2, 6*6=4*4+5*2*2
    for(int i=0;i<a[3]*5;i++)
    {
        if(a[1]==0||remain==0)
            break;
        else 
        {
            a[1]--;
            remain=remain-4;//装了2*2    剩余格子数    
        }
    }
    //在剩余格子中装入1*1 
    while(a[0]!=0&&remain!=0)
    {
            remain--;
            a[0]--;
    }
         return num;
}
//装入3*3 
int packet3(int a[6],int num)//6*6=4*3*3=1*3*3+5*2*2+7*1*1=2*3*3+3*2*2+6*1*1+3*3*3+1*2*2+5*1*1
{
    num+=a[2]/4;
    int re3=a[2]%4;//剩下的3*3个数,只可能是1,2,3,且只占一个包裹 
    //剩下了3个 
    //6*6=3*3*3+1*2*2+5*1*1
    if(re3==3) 
    {    
        num++;
        int remain=9;
        //装入2*2的包裹 
        for(int i=0;i<1;i++)
      {
            if(a[1]==0||remain==0)
                break;
            else 
            {
                a[1]--;
                remain=remain-4;//装了2*2    剩余格子数    
            }
      }
      //在剩余格子中装入1*1 
        while(a[0]!=0&&remain!=0)
        {
                remain--;
                a[0]--;
        }
         
    }
    //剩下了2个 
    //6*6=2*3*3+3*2*2+6*1*1
    if(re3==2)
    {
        num++;
        int remain=18;
        //装入2*2的包裹 
        for(int i=0;i<3;i++)
      {
            if(a[1]==0||remain==0)
                break;
            else 
            {
                a[1]--;
                remain=remain-4;//装了2*2    剩余格子数    
            }
      }
      //在剩余格子中装入1*1 
        while(a[0]!=0&&remain!=0)
        {
                remain--;
                a[0]--;
        }
        
    }
    //剩下了1个 
    //6*6=1*3*3+5*2*2+7*1*1
    if(re3==1)
    {
        num++;
        int remain=27;
        //装入2*2的包裹 
        for(int i=0;i<5;i++)
       {
            if(a[1]==0||remain==0)
                break;
            else 
            {
                a[1]--;
                remain=remain-4;//装了2*2    剩余格子数    
            }
       }
      //在剩余格子中装入1*1 
        while(a[0]!=0&&remain!=0)
        {
            remain--;
            a[0]--;
        }
    }
        return num; 
}
//装入2*2 
int packet2(int a[6],int num)//6*6=2*2*9
{
    num+=a[1]/9;
    int re2=a[1]%9;//剩余2*2
    //如果有剩下的,还需一个包裹 
    if(re2!=0)
    {
        num++;
        int remain=36-re2*4;
         while(remain!=0&&a[0]!=0) //用1*1填充 
         {
             remain--;
             a[0]--;
         }    
         
    }
        return num;
    
} 
//装入1*1 
int packet1(int a[6],int num)//6*6=36*1*1
{
    num+=a[0]/36;//剩下的1*1 需要的整包裹数 
    int re1=a[0]%36;//若多了,再用一个 
    if(re1!=0)
    num+=1;
    return num;
} 
 
 
int main()
{
    int a[6];
    while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5])
    {
        if(a[0]==0&&a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0&&a[5]==0)
        break;
        int num1=packet6(a,0);
        int num2= packet5(a,num1);
        int num3=  packet4(a,num2);
        int num4= packet3(a,num3);
        int num5=packet2(a,num4);
        int num6= packet1(a,num5);
        cout<<num6<<endl;
         
    }
    return 0; 
}

简短方法:

//Memory Time 
//248K   32MS 
 
#include<iostream>
using namespace std;
 
int max(int a,int b)
{
    return a>b?a:b;
}
 
int main(void)
{
    int s1,s2,s3,s4,s5,s6;      //6种size的盒子数量
    while(cin>>s1>>s2>>s3>>s4>>s5>>s6 && (s1+s2+s3+s4+s5+s6))
    {
        int BoxNum=0;           //放进所有盒子所需的最少箱子数
 
        BoxNum+=s6;             //6*6的盒子,每个都刚好独占一个箱子
 
        BoxNum+=s5;             //5*5的盒子,放进箱子后,每个箱子余下的空间只能放11个1*1的盒子
        s1=max(0,s1-s5*11);     //把1*1的盒子尽可能地放进已放有一个5*5盒子的箱子
 
        BoxNum+=s4;             //4*4的盒子,放进箱子后,每个箱子余下的空间为5个2*2的盒子空间
                                //先把所有2*2的盒子尽可能地放进这些空间
        if(s2>=s4*5)             //若2*2的盒子数比空间多
            s2-=s4*5;           //则消去已放进空间的部分
        else                    //若2*2的盒子数比空间少
        {                       //则先把所有2*2的盒子放进这些空间
            s1=max(0,s1-4*(s4*5-s2));   //再用1*1的盒子填充本应放2*2盒子的空间
            s2=0;               //一个2*2空间可放4个1*1盒子
        }
 
        BoxNum+=(s3+3)/4;       //每4个3*3的盒子完全独占一个箱子
        s3%=4;            //3*3的盒子不足4个时,都放入一个箱子,剩余空间先放2*2,再放1*1
        if(s3)
        {                       //当箱子放了i个3*3盒子,剩下的空间最多放j个2*2盒子
            if(s2>=7-2*s3)       //其中i={1,2,3} ; j={5,3,1}  由此可得到条件的关系式
            {
                s2-=7-2*s3;
                s1=max(0,s1-(8-s3));  //当箱子放了i个3*3盒子,并尽可能多地放了个2*2盒子后
            }                         //剩下的空间最多放j个1*1盒子,其中i={1,2,3} ; j={7,6,5}
            else                //但当2*2的盒子数不足时,尽可能把1*1盒子放入剩余空间
            {  //一个箱子最多放36个1*1,一个3*3盒子空间最多放9个1*1,一个2*2盒子空间最多放4个1*1
                s1=max(0,s1-(36-9*s3-4*s2));    //由此很容易推出剩余空间能放多少个1*1
                s2=0;
            }
        }
 
        BoxNum+=(s2+8)/9;       //每9个2*2的盒子完全独占一个箱子
        s2%=9;            //2*2的盒子不足9个时,都放入一个箱子,剩余空间全放1*1
        if(s2)
            s1=max(0,s1-(36-4*s2));
 
        BoxNum+=(s1+35)/36;     //每36个1*1的盒子完全独占一个箱子
 
        cout<<BoxNum<<endl;
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/fantastic123/p/9379019.html