POJ1017 Packets(貪心算法訓練)


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM