POJ 1017 Packets

Packets
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 39956   Accepted: 13361

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的六种物品,你有面积为6*6的背包(物品的高可以不管)

求要装下给定数量的六种物品,最少要用多少个背包

考虑6*6的物品要独占一个背包,5*5的物品可以和1*1的物品组合,4*4的物品可以与2*2或1*1的物品组合,3*3的物品可以每4个占一个背包。。。

这样贪心的方法从大到小取,模拟此过程算出最少用多少个背包

 

  1 #include<iostream>
  2 #include<cstdio>
  3 
  4 using namespace std;
  5 
  6 int a,b,c,d,e,f;
  7 
  8 int main()
  9 {
 10     while(scanf("%d %d %d %d %d %d",&a,&b,&c,&d,&e,&f)==6)
 11     {
 12         if(a==0&&b==0&&c==0&&d==0&&e==0&&f==0)
 13             break;
 14         int ans=e+f;
 15         a-=11*e;
 16         if(a<0)
 17             a=0;
 18         ans+=d;
 19         if(b>=5*d)
 20             b-=5*d;
 21         else
 22         {
 23             a-=20*d-4*b;
 24             if(a<0)
 25                 a=0;
 26             b=0;
 27         }
 28         if(c%4==0)
 29             ans+=c/4;
 30         else
 31         {
 32             ans+=c/4+1;
 33             if(c%4==1)
 34             {
 35                 if(b>=5)
 36                 {
 37                     b-=5;
 38                     a-=7;
 39                     if(a<0)
 40                         a=0;
 41                 }
 42                 else
 43                 {
 44                     a-=27-4*b;
 45                     if(a<0)
 46                         a=0;
 47                     b=0;
 48                 }
 49             }
 50             else if(c%4==2)
 51             {
 52                 if(b>=3)
 53                 {
 54                     b-=3;
 55                     a-=6;
 56                     if(a<0)
 57                         a=0;
 58                 }
 59                 else
 60                 {
 61                     a-=18-4*b;
 62                     if(a<0)
 63                         a=0;
 64                     b=0;
 65                 }
 66             }
 67             else if(c%4==3)
 68             {
 69                 if(b>=1)
 70                 {
 71                     b-=1;
 72                     a-=5;
 73                     if(a<0)
 74                         a=0;
 75                 }
 76                 else
 77                 {
 78                     a-=9;
 79                     if(a<0)
 80                         a=0;
 81                     b=0;
 82                 }
 83             }
 84         }
 85         if(b%9==0)
 86             ans+=b/9;
 87         else
 88         {
 89             ans+=b/9+1;
 90             a-=36-4*(b%9);
 91             if(a<0)
 92                 a=0;
 93             b=0;
 94         }
 95         if(a%36==0)
 96             ans+=a/36;
 97         else
 98             ans+=a/36+1;
 99 
100         cout<<ans<<endl;
101     }
102 
103     return 0;
104 }
[C++]

 

原文地址:https://www.cnblogs.com/lzj-0218/p/3246524.html