A factory produces products packed in square packets of the same height h and of the sizes ,
,
,
,
,
. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size
. 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 to the biggest size
. 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
1个5*5+11个1*1
1个4*4+5个2*2(有空隙时优先放置2*2,如果没放完2*2的,剩下的就放置1*1)
放置3*3时,组合情况比较复杂。 没有放完3*3时,剩下的空隙也是优先尽可能多地放置2*2
当放置1个3*3时,还可以放置7个1*1和5个2*2
当放置2个3*3时,还可以放置6个1*1和3个2*2
当放置3个3*3时,还可以放置5个1*1和1个2*2
因为一个4*4,5*5,6*6只能放置在一个盒子里,所以有多少个这些,就需要多少个盒子来装。
然后因为3*3的可以和1*1和2*2的组合放置,所以也可以确定装完3*3需要的盒子。
那么在计算时,首先就可以确定放完3*3,4*4,5*5,6*6的盒子个数,假设是t个盒子。
然后这t个盒子中会有空隙,再优先把2*2的放置到t个盒子中的空隙中。如果这些空隙不能放完2*2的,那么就需要再增加盒子知道放完为止。最后在考虑放置1*1。
#include<iostream>
#include <cstdio>
#include<algorithm>
#include <cmath>
using namespace std;
int main()
{
int a[7],i,j,k,n,t;
while(~scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]))
{
if(!a[1]&&!a[2]&&!a[3]&&!a[4]&&!a[5]&&!a[6])
break;
n=0;
n+=a[6];
n+=a[5];
a[1]-=11*a[5];
n+=a[4];
a[2]-=5*a[4];
n+=a[3]/4;
t=a[3]%4;
if(t==3)
{
a[1]-=5;
a[2]-=1; n++;
}
else
if(t==2)
{
a[1]-=6;a[2]-=3;
n++;
}
else
if(t==1)
{
a[1]-=7;
a[2]-=5;
n++;
}
if(a[2]<0)
{
a[1]+=a[2]*4;
a[2]=0;
}
if(a[1]<0)
a[1]=0;
n+=(a[1]+4*a[2])/36;
if((a[1]+4*a[2])%36!=0)
n++;
cout<<n<<endl;
}
return 0;
}