poj 1017

Packets
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 43359 Accepted: 14611

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*1, 2*2, 3*3, 4*4, 5*5, 6*6。

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

解题思路 

    这个问题描述得比较清楚,我们在这里只解释一下输入输出样例:共有两组有效输入,第一组表示有4 个3*3 的产品和一个6*6 的产品,此时4 个 3*3 的产品占用一个箱子,另外 一个 6*6 的产品占用 1 个箱子,所以箱子数是 2;第二组表示有7 个 1*1 的产品,5 个 2*2 的产品和1 个 3*3 的产品,我们可以把他们统统放在一个箱子中,所以输出是1。 
    分析六个型号的产品占用箱子的具体情况如下:6*6的产品每个会占用一个完整的箱子,并且没有空余空间;5*5  的产品每个占用一个新的箱子,并且留下 11 个可以盛放 1*1的产品的空余空间;4*4 的产品每个占用一个新的箱子,并且留下5 个可以盛放2*2 的产品的空余空间;3*3 的产品情况比较复杂,首先3*3 的产品不能放在原来盛有5*5 或者4*4 的箱子中,那么必须为3*3 的产品另开新的箱子,新开的箱子数目等于3*3 的产品的数目除以 4  向上取整;同时我们需要讨论为3*3 的产品新开箱子时,剩余的空间可以盛放多少2*2 和1*1 的产品(这里如果有空间可以盛放2*2 的产品,我们就将它计入2*2 的空余空间,等到2*2  的产品全部装完,如果还有2*2  的空间剩余,再将它们转换成 1*1 的剩余空间)。我们可以分情况讨论为3*3 的产品打开的新箱子中剩余的空位,共为四种情况:第一种,3*3 的产品的数目正好是4 的倍数,所以没有空余空间;第二种,3*3 的产品数目是4 的倍数加1, 

这时还剩 5 个2*2 的空位和7 个 1*1 的空位;第三种,3*3 的产品数目是4 的倍数加2,这时还剩3 个2*2 的空位和6 个 1*1 的空位;第四种,3*3 的产品数目是4 的倍数加3,这时还剩 1 个 2*2  的空位和5 个 1*1 的空位;处理完3*3  的产品,就可以比较一下剩余的2*2的空位和2*2 产品的数目,如果产品数目多,就将2*2 的空位全部填满,再为2*2 的产品打开新箱子,同时计算新箱子中 1*1 的空位,如果剩余空位多,就将2*2 的产品全部填入2*2 
的空位,再将剩余的 2*2  的空位转换成 1*1 的空位;最后处理 1*1 的产品,比较一下 1*1 .的空位与1*1 的产品数目,如果空位多,将1*1 的产品全部填入空位,否则,先将1*1 的空 位填满,然后再为 1*1 的产品打开新的箱子。

 
 
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. int max(int a,int b)  
  5. {  
  6.     return a>b?a:b;  
  7. }  
  8.   
  9. int main(void)  
  10. {  
  11.     int s1,s2,s3,s4,s5,s6;      //6种size的盒子数量  
  12.     while(cin>>s1>>s2>>s3>>s4>>s5>>s6 && (s1+s2+s3+s4+s5+s6))  
  13.     {  
  14.         int BoxNum=0;           //放进所有盒子所需的最少箱子数  
  15.   
  16.         BoxNum+=s6;             //6*6的盒子,每个都刚好独占一个箱子  
  17.   
  18.         BoxNum+=s5;             //5*5的盒子,放进箱子后,每个箱子余下的空间只能放11个1*1的盒子  
  19.         s1=max(0,s1-s5*11);     //把1*1的盒子尽可能地放进已放有一个5*5盒子的箱子  
  20.   
  21.         BoxNum+=s4;             //4*4的盒子,放进箱子后,每个箱子余下的空间为5个2*2的盒子空间  
  22.                                 //先把所有2*2的盒子尽可能地放进这些空间  
  23.         if(s2>=s4*5)             //若2*2的盒子数比空间多  
  24.             s2-=s4*5;           //则消去已放进空间的部分  
  25.         else                    //若2*2的盒子数比空间少  
  26.         {                       //则先把所有2*2的盒子放进这些空间  
  27.             s1=max(0,s1-4*(s4*5-s2));   //再用1*1的盒子填充本应放2*2盒子的空间  
  28.             s2=0;               //一个2*2空间可放4个1*1盒子  
  29.         }  
  30.   
  31.         BoxNum+=(s3+3)/4;       //每4个3*3的盒子完全独占一个箱子  
  32.         s3%=4;            //3*3的盒子不足4个时,都放入一个箱子,剩余空间先放2*2,再放1*1  
  33.         if(s3)  
  34.         {                       //当箱子放了i个3*3盒子,剩下的空间最多放j个2*2盒子  
  35.             if(s2>=7-2*s3)       //其中i={1,2,3} ; j={5,3,1}  由此可得到条件的关系式  
  36.             {  
  37.                 s2-=7-2*s3;  
  38.                 s1=max(0,s1-(8-s3));  //当箱子放了i个3*3盒子,并尽可能多地放了个2*2盒子后  
  39.             }                         //剩下的空间最多放j个1*1盒子,其中i={1,2,3} ; j={7,6,5}  
  40.             else                //但当2*2的盒子数不足时,尽可能把1*1盒子放入剩余空间  
  41.             {  //一个箱子最多放36个1*1,一个3*3盒子空间最多放9个1*1,一个2*2盒子空间最多放4个1*1  
  42.                 s1=max(0,s1-(36-9*s3-4*s2));    //由此很容易推出剩余空间能放多少个1*1  
  43.                 s2=0;  
  44.             }  
  45.         }  
  46.   
  47.         BoxNum+=(s2+8)/9;       //每9个2*2的盒子完全独占一个箱子  
  48.         s2%=9;            //2*2的盒子不足9个时,都放入一个箱子,剩余空间全放1*1  
  49.         if(s2)  
  50.             s1=max(0,s1-(36-4*s2));  
  51.   
  52.         BoxNum+=(s1+35)/36;     //每36个1*1的盒子完全独占一个箱子  
  53.   
  54.         cout<<BoxNum<<endl;  
  55.     }  
  56.     return 0;  
  57. }  
 
 
 
 
  1. #include <stdio.h>   
  2. void main()   
  3. {   
  4.  int N, a, b, c, d, e, f, y, x;//N用来存储需要的箱子数目,y用来存储 2*2 的空位数目   
  5.                          // x 用来存储 1*1 的空位数目。   
  6.  int u[4]={0, 5, 3, 1};        
  7. //数组u 表示3*3 的产品数目分别是 4的倍数,4 的倍数+1, 4 的倍数+2, 4 的倍数+3   
  8. //时,为3*3的产品打开的新箱子中剩余的 2*2的空位的个数   
  9.    
  10.  while(1){   
  11.   scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f);   
  12.   if (a == 0 && b == 0 && c == 0 && d == 0 && e == 0 && f == 0) break;   
  13.   N = f + e + d + (c + 3) / 4;     
  14. //这里有一个小技巧 - (c+3)/4  正好等于 c 除以4向上取整的结果,下同    
  15.   y = 5 * d + u[c % 4];//每一个4*4的箱子装过后还可以再装5个2*2的箱子  
  16.   //还有3*3的箱子如果没填满6*6的箱子的话,也可以用来装2*2的箱子  
  17.   //5*5的箱子则只能装1*1的情况了  
  18.    if(b > y) N += (b - y + 8 ) / 9;//如果要装的2*2的箱子的数目比提供的空间要多,  
  19.    //那么多的部分要新开一些箱子   
  20.   x = 36 * N - 36 * f - 25 * e - 16 * d - 9 * c - 4 * b;   
  21.   if(a > x) N += ( a - x + 35 ) / 36;   
  22.   printf("%d/n", N);   
  23.  }   
  24. }   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值