POJ 1017Packets(贪心)
Packets
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 40544 | Accepted: 13594 |
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
题目大意:做出来的产品高度为h,长宽一样,有1*1,2*2,3*3,4*4,5*5,6*6,六种规格,把他们装进6*6高h的箱子中,问你最少需要多少箱子。
解题思路:6*6的直接填进去,5*5的还可以放11个1*1的,4*4的可以放5个2*2的2*2的用完了,可以放1*1的,3*3的每四个放一个箱子里,如果还剩的有,那就分情况讨论,往里面塞2*2和1*1的。最后只剩下2*2和1*1就可与随便装了!具体见代码。
题目地址:Packets
AC代码:
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> using namespace std; int main() { int a,b,c,d,e,f; int res; while(scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f)) { if(!a&&!b&&!c&&!d&&!e&&!f) break; res=f; //6*6的直接填满 res+=e; if(a<=e*11) a=0; //一个5*5的可以再塞11个1*1的 else a-=e*11; res+=d; if(b>d*5) b-=d*5; else //2*2的用完了 { int tt=(d*5-b)*4; b=0; if(a>tt) a=a-tt; //放1*1的 else a=0; } res+=c/4; //每四个3*3组成1个6*6 int tmp=c%4; if(tmp) //表示还有剩余3*3的 { res++; if(tmp==1) { if(b>5) { b-=5; //放2*2 if(a>7) a-=7; //放1*1 else a=0; } else { int tt=(5-b)*4; b=0; tt+=7; if(a>tt) a-=tt; else a=0; } } else if(tmp==2) { if(b>3) { b-=3; //放2*2 if(a>6) a-=6; //放1*1 else a=0; } else { int tt=(3-b)*4; b=0; tt+=6; if(a>tt) a-=tt; else a=0; } } else if(tmp==3) { if(b>1) { b-=1; //放2*2 if(a>5) a-=5; //放1*1 else a=0; } else { int tt=(1-b)*4; b=0; tt+=5; if(a>tt) a-=tt; else a=0; } } } tmp=a+b*4; //到最后全是1*1和2*2的就可以直接填了 res+=tmp/36; if(tmp%36) res++; printf("%d\n",res); } return 0; } //596 21 348 9 8 4