POJ-1017(贪心)
Packets
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 49267 | Accepted: 16685 |
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
题意:有6种物品,大小分别是1*1,2*2,……6*6。有一种盒子,是6*6的。问,给出每种物品的数目,求最少用几个盒子能把所有的物品装下。
思路:贪心。分类考虑。物品4,5,6一件物品一个盒子。然后3是4件物品一个盒子。2的话,先看能不能在4物品的盒子装下,然后看3,多余的重新装盒子。1类似。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 using namespace std; 6 7 //每种物品的数量 8 int num[6]; 9 //对应大小盒子所需数量 10 int cnt[6]; 11 //相应大小盒子剩余的1*1的格子数。 12 int remain[6]; 13 14 int main() 15 { 16 while(1){ 17 for (int i = 0; i < 6; i++){ 18 scanf("%d", &num[i]); 19 } 20 if(num[0]+num[1]+num[2]+num[3]+num[4]+num[5] == 0) break; 21 22 memset(cnt, 0, sizeof(cnt)); 23 memset(remain, 0, sizeof(remain)); 24 25 for (int i = 5; i >= 0; i--){ 26 //如果物品大小是4,5,6,每个物品都需要一个盒子 27 if(i >= 3){ 28 cnt[i] = num[i]; 29 remain[i] = cnt[i]*((5-i)*6*2 - (5-i)*(5-i)); 30 } 31 //如果物品大小是3,每4个物品需要一个盒子。 32 else if(i == 2){ 33 cnt[i] = ceil(num[i]/4.0); 34 remain[3] = (num[2] % 4)*9; 35 } 36 //如果物品是2,需要判断4物品的剩余空间,3物品的剩余空间。 37 else if(i == 1){ 38 //如果4物品剩余的空间能够放下所有的2。 39 if(cnt[3]*5 >= num[i]){ 40 remain[3] = (cnt[3] * 5 - num[i]) * 4; 41 } 42 //如果4物品的剩余空间放不下所有2。 43 else { 44 //放一部分2物品在4物品剩余空间之后剩下的2物品数量 45 int num1 = num[i] - cnt[3]*5; 46 remain[3] = 0; 47 //3物品盒子的三种情况。先假设都能填满。 48 if(num[2] % 4 == 1){ num1-= 5; remain[2] = 7;} 49 else if(num[2] % 4 == 2){ num1-= 3; remain[2] = 6;} 50 else if(num[2] % 4 == 3){ num1-= 1; remain[2] = 5;} 51 //如果是负数,说明3物品的盒子能够装下剩下的所有2物品。更新remain 52 if(num1 <= 0){ 53 remain[2] += (-1*num1*4); 54 } 55 //如果是正数,说明2物品还有剩余。需要重新装盒子。 56 else{ 57 cnt[i] = ceil(num1 / 9.0); 58 remain[i] = ((9-(num1 % 9)) * 4); 59 } 60 } 61 } 62 //如果物品是1,只需判断所有的remain,是否大于1物品的数量。 63 else if(i == 0){ 64 int he = 0; 65 for(int i = 0; i < 6; i++){ 66 he += remain[i]; 67 } 68 //如果1物品的数量大于所有的remain,则需要再装一个盒子。 69 if(he < num[i]){ 70 cnt[i] = ceil((num[i]-he) / 36.0); 71 } 72 } 73 } 74 75 int result = 0; 76 for (int i = 0; i < 6; i++){ 77 result += cnt[i]; 78 } 79 80 printf("%d\n", result); 81 } 82 return 0; 83 }

浙公网安备 33010602011771号