首页 > 代码库 > poj 1017 Packets
poj 1017 Packets
Packets
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 43436 | Accepted: 14637 |
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,5×5和4×4的每个都需要一个箱子
5×5的和11个1×1的装一起,4×4的和5个2×2的装一起
3×3的分4种情况
1.正好装满
2.剩一个,则装5个2×2的,7个1×1的
3.剩两个,则装3个2×2,6个1×1的
4.剩三个,则装1个2×2的,5个1×1的
还要多余的2×2的,装完后用1×1的填充
若2×2的不够,原来用2×2的用1×1的填充
#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; int p[4]={0,5,3,1}; //3×3的放完后,余下的放入新箱子后,还可以放几个2×2的包裹(下标对应余数) int main () { int a,b,c,d,e,f; int k1,k2,sum; while(cin>>a>>b>>c>>d>>e>>f) { if(a==0&&b==0&&c==0&&d==0&&e==0&&f==0) break; sum=f+e+d+(c+3)/4; // 6*6,5*5,4*4每个都要用一个箱子,3*3的对4向上取整 k1=d*5+p[c%4]; // 把2*2和4*4(3*3)放在一起时,需要2*2的个数 if(k1<b) sum+=(b-k1+8)/9; // 单独把2*2放一个箱子时,需要2*2的个数 k2=sum*36-b*4-c*9-d*16-e*25-f*36; // 需要1*1的个数 if(k2<a) sum+=(a-k2+35)/36; //单独把1*1放一个箱子时,需要1*1的个数 cout<<sum<<endl; } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。