UVA - 311 Packets(贪心)


 Packets 

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种包裹,分别为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;
}


你可能感兴趣的:(uva,packets,311)