UVA - 311 Packets(贪心)


 Packets 

A factory produces products packed in square packets of the same height h and of the sizes tex2html_wrap_inline27 , tex2html_wrap_inline29 , tex2html_wrap_inline31 , tex2html_wrap_inline33 , tex2html_wrap_inline35 , tex2html_wrap_inline37 . These products are always delivered to customers in the square parcels of the same height h as the products have and of the size tex2html_wrap_inline37 . 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 tex2html_wrap_inline27 to the biggest size tex2html_wrap_inline37 . 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种包裹,分别为1*1、2*2、3*3……6*6,每种包裹的高度相同都为h,现在要将这些包裹装进6*6的箱子中,问最少需要多少个6*6的箱子。

解析:贪心,不难想到应该先装大的包裹,再装小的包裹。
所以按照这个思路来分析一下该题。不妨把每种情况画出来。

(1)6*6,1个6*6
(2)5*5,1个5*5+11个1*1
(3)4*4,1个4*4+5个2*2
(4)3*3应该分4种情况讨论,

4个3*3

1个3*3+5个2*2+7个1*1

2个3*3+3个2*2+6个1*1

3个3*3+1个2*2+5个1*1

(5)2*2
9个2*2
(6)1*1
36个1*1

注意上面的每种情况,每次用到2*2的包裹就要判断,如果2*2的包裹用完的话,要用1*1的包裹来填充。

#include <cstdio>
#include <cstring>
using namespace std;
int a[7];
void judge() {
	if(a[2] < 0) {
		a[1] += 4 * a[2];
		a[2] = 0;
	}
}
int main() {
	int sum,tmp;
	while(1) {
		bool ok = false;
		sum = 0;
		for(int i = 1; i <= 6; i++) {
			scanf("%d",&a[i]);
			ok = ok || a[i];
		}
		if(!ok) {
			break;
		}
		//6*6
		sum += a[6];
		//5*5
		sum += a[5];
		a[1] -= 11 * a[5];
		//4*4
		sum += a[4];
		a[2] -= 5 * a[4];
		judge();
		//3*3
		sum += a[3] / 4;
		tmp = a[3] % 4;
		if(tmp) {
			sum++;
			switch(tmp) {
				case 1:
					a[2] -= 5;
					a[1] -= 7;
					judge();
					break;
				case 2:
					a[2] -= 3;
					a[1] -= 6;
					judge();
					break;
				case 3:
					a[2] -= 1;
					a[1] -= 5;
					judge();
					break;
			}
		}
		//2*2
		if(a[2] > 0) {
			sum += a[2] / 9;
			tmp = a[2] % 9;
			if(tmp) {
				sum++;
			}
			a[1] -= (36 - tmp*4);
		}
		//1*1
		if(a[1] > 0) {
			sum += a[1] / 36;
			tmp = a[1] % 36;
			if(tmp) {
				sum++;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值