Uva--311(贪心)

2014-07-23 20:57:11

 Packets 

A factory produces products packed in square packets of the same height h and of the sizes tex2html_wrap_inline27 , tex2html_wrap_inline29 , tex2html_wrap_inline31 , tex2html_wrap_inline33 , tex2html_wrap_inline35 , tex2html_wrap_inline37 . These products are always delivered to customers in the square parcels of the same height h as the products have and of the size tex2html_wrap_inline37 . 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 tex2html_wrap_inline27 to the biggest size tex2html_wrap_inline37 . 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 /*************************************************************************
 2     > File Name: Uva311.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Wed 23 Jul 2014 09:26:39 AM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 int main(){
17     int si[10],sum;
18     while(scanf("%d%d%d%d%d%d",&si[1],&si[2],&si[3],&si[4],&si[5],&si[6]) == 6){
19         if(si[1] + si[2] + si[3] + si[4] + si[5] + si[6] == 0)
20             break;
21         sum = si[6];
22         if(si[5]){
23             sum += si[5];
24             si[1] -= si[5] * 11;
25             if(si[1] < 0) si[1] = 0;
26         }
27         if(si[4]){
28             sum += si[4];
29             int k = si[4] * 5;
30             if(si[2] >= k){
31                 si[2] -= k;
32                 k = 0;
33             }
34             else{
35                 k -= si[2];
36                 si[2] = 0;
37             }
38             if(k && si[1]){
39                 k *= 4;
40                 si[1] -= k;
41                 if(si[1] < 0)
42                     si[1] = 0;
43             }
44         }
45         if(si[3]){
46             int k = si[3] / 4;
47             sum += k;
48             si[3] -= k * 4;
49             if(si[3]){
50                 sum += 1;
51                 int tk;
52                 k = 36 - si[3] * 9;
53                 if(si[3] == 3) tk = 1;
54                 else if(si[3] == 2) tk = 3;
55                 else tk = 5;
56                 if(si[2]){
57                     if(si[2] >= tk){
58                         si[2] -= tk;
59                         k -= tk * 4;
60                         tk = 0;
61                     }
62                     else{
63                         tk -= si[2];
64                         k -= si[2] * 4;
65                         si[2] = 0;
66                     }
67                 }
68                 if(k && si[1]){
69                     si[1] -= k;
70                     if(si[1] < 0)
71                         si[1] = 0;
72                 }
73             }
74         }
75         if(si[2]){
76             int k = si[2] / 9;
77             sum += k;
78             si[2] -= k * 9;
79             if(si[2]){
80                 sum += 1;
81                 int k = 36 - si[2] * 4;
82                 if(si[1]){
83                     si[1] -= k;
84                     if(si[1] < 0) si[1] = 0;
85                 }
86             }
87         }
88         if(si[1]){
89             int k = si[1] / 36;
90             sum += k;
91             si[1] -= k * 36;
92             if(si[1]) sum += 1;
93         }
94         printf("%d\n",sum);
95     }
96     return 0;
97 }

 



posted @ 2014-07-23 20:59  Naturain  阅读(105)  评论(0编辑  收藏  举报