Packets(思维)

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的产品,每个产品放入一个箱子,剩余空间允许放入的最大尺寸为1*1,且最多放11个
4*4的产品,每个产品放入一个箱子,该箱子的剩余空间允许放入的最大尺寸为2*2,且最多放入5个
3*3的产品,由于一个箱子最多可放4个产品,需要分情况考虑(初思考分析时可能感觉有点乱)

然后2*2,1*1.

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=5e6+5;
ll a[10];
int main()
{
	ll x,n,t,y;
	while(1)
	{
		cin>>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;
		ll s=a[6];
		while(a[5]>0&&a[1]>0)//打包5*5产品时顺便捎上1*1产品
		{
			s++;
			a[5]--;
			a[1]-=11;
		}
		while(a[5]>0)s++,--a[5];//1*1产品已装完,5*5产品还没装完
		while(a[4]>0&&a[2]>0)//同上
		{
			s++;
			a[4]--;
			a[2]-=5;
		}
		while(a[4]>0&&a[1]>0)//同上
		{
			s++;
			a[4]--;
			a[1]-=20;
		}
		while(a[4]>0)s++,a[4]--;//同上
		while(a[3]>=4)s++,a[3]-=4;// 先凑整的,即4个3*3的正好装满一个6*6的
		if(a[2]<0)a[2]=0;
		if(a[3]==1)//3*3产品还剩下一个
		{
			if(a[2]<=5)//尽量先装大的,即能装2*2的就不装1*1的
			{
				x=6*6-2*2*a[2]-3*3;//剩余空间,留给1*1产品
				a[1]-=x;
				a[2]=0;
				s++;
			}
			else 
			{
				x=6*6-2*2*5-3*3;
				a[1]-=x;
				a[2]-=5;
				s++;
			}
		}
		else if(a[3]==2)
		{
			if(a[2]<=3)
			{
				x=6*6-2*2*a[2]-3*3*2;
				a[1]-=x;
				a[2]=0;
				s++;
			}
			else 
			{
				x=6*6-2*2*3-3*3*2;
				a[1]-=x;
				a[2]-=3;
				s++;
			}
		}
		else if(a[3]==3)
		{
			if(a[2]<=1)
			{
				x=6*6-3*3*3-2*2*a[2];
				a[2]=0;
				a[1]-=x;
				s++;
			}
			else
			{
				x=6*6-3*3*3-2*2;
				a[2]-=1;
				a[1]-=x;
				s++;
			}
		}
		while(a[2]>=9)a[2]-=9,s++;
		if(a[2]>0)
		{
			x=6*6-2*2*a[2];
			a[1]-=x;
			s++;
		}
		while(a[1]>=36)a[1]-=36,s++;
		if(a[1]>0)s++;
		printf("%lld\n",s);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_52898168/article/details/120618756
今日推荐