poj1017
MATWETU
posted @ 2011年4月09日 21:33
in 腊雜
, 884 阅读
Packets
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29616 | Accepted: 9757 |
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*6的格子,可以放下:1个6*6 or 1个5*5+11个1*1 or 1个4*4+5个2*2
由于边长4 5 6的只能填进一个洞,直接加上就是了,然后空隙尽量填满,大的优先(最大的只有2,1总能填到2里)
剩下只有边长3的,两种情况:4个3*3 和 不足4个3*3的(-_-b)
算一下可以发现,能把4个3*3分开装 和 先装4个3*3再装别的 比,效果是一样的。。。于是就先按4个3*3的方式塞满。。。
剩下1?2?3? 个3*3的。。。算一下(-_-b)也能得出,放一块的效果不必放一起差。。。
然后。。。就没有然后了。。。2和1,照算可也
附自己看不懂的代码:
#include <stdlib.h> #include <stdio.h> #include <string.h> #define dill do{if(s[2]<0)s[1]-=4*(-s[2]);if(s[1]<0)s[1]=0;if(s[2]<0)s[2]=0;}while(0) int main ( int argc, char *argv[] ) { int s[7],i,sum=0,t[3][2]={{1,5},{3,6},{5,7}}; for(;;) { sum=0; for(i=1;i<=6;i++){scanf("%d",&s[i]);sum+=s[i];} if(sum==0)break; sum=0; sum+=s[6]+s[5]+s[4]+s[3]/4; s[1]-=11*s[5]; s[2]-=5*s[4]; dill; s[3]%=4; if(s[3]){ s[2]-=t[3-s[3]][0]; s[1]-=t[3-s[3]][1]; sum++; dill; } sum+=s[2]/9; s[2]%=9; if(s[2]){ s[1]-=36-s[2]*4; sum++; } if(s[1]>0)sum+=s[1]/36+(s[1]%36>0?1:0); printf("%d\n",sum); } return EXIT_SUCCESS; }
2024年1月14日 19:17
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit