码迷,mamicode.com
首页 > 编程语言 > 详细

POJ1017 Packets(贪心算法训练)

时间:2016-07-17 23:08:45      阅读:560      评论:0      收藏:0      [点我收藏+]

标签:

Time Limit: 1000MS          Memory Limit: 10000K          Total Submissions: 51306          Accepted: 17391

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 

一、题目大意

      公司共有底面面积为1*1、2*2、3*3、4*4、5*5、6*6,高度同为H的六种产品,现在需要用最少的箱子打包,箱子的底面面积为6*6,高度为H。

 

二、解题思路

     简单的暴力贪心算法,对不同的产品有不同的策略,按照从大到小的顺序打包产品,策略如下:

     6*6:1个产品放在1个箱子里

     5*5:1个产品要占用1个箱子,用1*1的箱子可以填充(11个填满1箱)

     4*4:1个产品要占用1个箱子,剩余空间用2*2和1*1的箱子填充(先填充2*2,再填充1*1)

     3*3:4个产品可以填满1个箱子,假如有不满1个箱子的,分情况用1*1和2*2的产品填满

     2*2:9个产品可以填满1个箱子,假如有不满1个箱子的,用1*1的产品填充

     1*1:36个产品可填满一个箱子

 

三、具体代码 

技术分享
 1 #include <cstdio>
 2 
 3 int MAX_(int a, int b){
 4     if(a>b) return a;
 5     else return b;
 6 }
 7 
 8 int main(){
 9     int s1, s2, s3, s4, s5, s6;
10     while(scanf("%d%d%d%d%d%d", &s1, &s2, &s3, &s4, &s5, &s6) && s1+s2+s3+s4+s5+s6){
11         int packets = 0;
12         packets += s6; // 6*6的产品一个装一箱
13         
14         packets += s5; // 5*5的产品一个装一箱
15         s1 = MAX_(0, s1-11*s5); // 剩余空间用1*1的产品尽量填满
16         
17         packets += s4; // 4*4的产品一个装一箱
18         if(s2<5*s4) s1 = MAX_(0, s1-(5*s4-s2)); // 假如2*2的产品填完之后仍然有空隙,则用1*1填满 
19         s2 = MAX_(0, s2-5*s4); // 尽量用2*2的产品填满 
20         
21         packets += (s3+3)/4; // 3*3的产品四个一箱
22         s3 %= 4;            // 假如3*3的箱子不是四的倍数个,则先用2*2填充再用1*1填充 
23         if(s3==1){
24             if(s2<5) s1 = MAX_(0, s1-(27-4*s2));
25             else     s1 = MAX_(0, s1-7);
26             s2 = MAX_(0, s2-5);
27         } 
28         else if(s3==2){
29             if(s2<3) s1 = MAX_(0, s1-(18-4*s2));
30             else     s1 = MAX_(0, s1-6);
31             s2 = MAX_(0, s2-3);
32         }
33         else if(s3==3){
34             if(s2<1) s1 = MAX_(0, s1-(9-4*s2));
35             else     s1 = MAX_(0, s1-5);
36             s2 = MAX_(0, s2-1);    
37         }
38         
39         packets += (s2+8)/9; // 2*2的产品九个一箱
40         s2 %= 9;             // 假如2*2的箱子不是九的倍数个,则用1*1填充
41         if(s2) s1 = MAX_(0, s1-(36-4*s2)); 
42         
43         packets += (s1+35)/36; // 1*1的产品三十六个一箱
44         
45         printf("%d\n", packets); 
46     } 
47     
48     return 0;
49 } 
View Code

 

POJ1017 Packets(贪心算法训练)

标签:

原文地址:http://www.cnblogs.com/jinglecjy/p/5679639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!