题目描述:
A factory produces products packed in square packets of the same height h and of the sizes 11, 22, 33, 44, 55, 66. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 66. 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.
输入描述:
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 11 to the biggest size 66. The end of the input file is indicated by the line containing six zeros.
输出描述:
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.
输入:
0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0
输出:
2
1
题意:
一个工厂制造的产品形状都是长方体盒子,它们的高度都是 h,长和宽都相等,一共有六个型号,分别为11, 22, 33, 44, 55, 66。
这些产品通常使用一个 66h 的长方体箱子包装然后邮寄给客户。因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的箱子数量。
题解:
66的盒子,每个盒子独占一个箱子。
55的盒子,每个盒子放入一个箱子,该箱子的剩余空间允许放入的最大尺寸为11,且最多放11个。
44的盒子,每个盒子放入一个箱子,该箱子的剩余空间允许放入的最大尺寸为22。
33的盒子,每4个刚好独占一个箱子,不足4个33的,剩下空间由22和12填充。
22的盒子和11的盒子主要用于填充其他箱子的剩余空间,填充后的多余部分才开辟新箱子装填。
其余看注解
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
int s1,s2,s3,s4,s5,s6;
while(scanf("%d%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5,&s6)!=EOF){
if(s1 == 0 && s2 == 0 && s3 == 0 && s4 == 0 && s5 == 0 && s6 == 0) break;
//处理体积为6
int sum = s6;
//处理体积为5
sum += s5;
s1 = max(0,s1 - s5 * 11);
//处理体积为4
sum += s4;
if(s2 >= s4 * 5){
s2 -= (s4 * 5);
}
else{
s1 = max(0,s1 - 4 * (s4 * 5 - s2));
s2 = 0;
}
//处理体积为3
sum += (s3 + 3) / 4;
s3 %= 4;
if(s3){ //当箱子放了i个3*3盒子,剩下的空间最多放j个2*2盒子
if(s2 >= 7 - 2 * s3){ //其中i={1,2,3} ; j={5,3,1} 由此可得到条件的关系式
s2 -= (7 - 2 * s3);
s1=max(0,s1 - (8 - s3)); //当箱子放了i个3*3盒子,并尽可能多地放了个2*2盒子后
} //剩下的空间最多放j个1*1盒子,其中i={1,2,3} ; j={7,6,5}
else{ //但当2*2的盒子数不足时,尽可能把1*1盒子放入剩余空间
//一个箱子最多放36个1*1,一个3*3盒子空间最多放9个1*1,一个2*2盒子空间最多放4个1*1
s1 = max(0,s1 - (36 - 9 * s3 - 4 * s2)); //由此很容易推出剩余空间能放多少个1*1
s2 = 0;
}
}
//处理体积为2
sum += (s2 + 8) / 9;
s2 %= 9;
if(s2) s1 = max(0,s1 - (36 - 4 * s2));
//处理体积为1
sum += (s1 + 35) /36;
cout<<sum<<endl;
}
return 0;
}