装箱问题(贪心)

≡放荡痞女 提交于 2020-03-27 06:03:35
Packets
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28329   Accepted: 9291

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

 
【题意】:

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

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

【思路】:我tm一直以为是先装小的啊啊啊,对啊,能装尽量装,把尽量多的产品放进去不就避免多开箱子,放尽量多的产品不就先放小的的么????然后我就不会做了,网上先放大的当时我???,然后推了推,如果 是8个1*1,和一个5*5,如果按我先放小的,那样就放不进5*5了,如果先放5*5,就可以放进去8个1*1了。。。。所以千万不要理所当然。。。(当然如果你一开始跟我一样想的话QAQ)

【代码君】

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 using namespace std;
 5 int room2[4]={0,5,3,1};
 6 int main()
 7 {    
 8     int s1,s2,s3,s4,s5,s6,n1,n2;
 9     while(scanf("%d%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5,&s6)&&s1||s2||s3||s4||s5||s6)//能够输入并且都不为0时 
10     {
11         int sum=0;//计算箱子总数0 
12         sum+=s6+s5+s4+(s3+3)/4;//6*6和5*5和4*4一定是一个占一个箱子,而3*3 4个占一个箱子 
13         n2=5*s4+room2[s3%4];//剩余的能够放2*2的空间,有5*5,6*6的箱子已经不能放2*2了,放了4*4的箱子可以放5个2*2+放3*3的箱子对应的2*2的空间 
14         if(s2>n2)//2*2的个数比我们留出来为2*2的空间个数多,就需要为2*2另开箱子 
15         {
16             sum+=(s2-n2+8)/9;//求出多需要几个2*2空间,+8是向上取整,再除以9,(因为每个箱子可以放9个2*2) 
17         }
18         n1=36*(sum-s6)-s2*2*2-s3*3*3-s4*4*4-s5*5*5;//用减法计算1*1剩余的空间 
19         if(s1>n1)//如果1*1的个数,比我们留出的空间多就需要另开空间 
20         sum+=(s1-n1+35)/36;//+35是向上取整,将多出来的另开箱子 
21         printf("%d\n",sum);
22     }
23     return 0;
24 }

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!