poj--1017——Packets

Packets
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 59032   Accepted: 20049

Description

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

题目大意:把一些高为h1*1,2*2,3*3,4*4,5*5,6*6的物品放进同样高的6*6的盒子里,每行输入六个数据代表

每种物品的个数,问最少需要多少个盒子来装这些物品。

这道题想了一下午,职业生涯规划课都没好好上想这道题去了,其实这道题的贪心很简单,最难的还是其中的细节处理

以及过程的模拟。贪心的思路是:放完5*5的盒子在用11个1*1的盒子填,放完4*4的盒子在用5个2*2的盒子填如果2*2的

盒子不够在用1*1的盒子填,一次最多可以放4个3*3的盒子,如果不够用2*2和1*1的盒子填(这里的坑比较多),最后剩下

的在放入新的盒子中。ac代码如下:

#include
#include
#include
using namespace std;
int main(){
	int n1,n2,n3,n4,n5,n6;
	int three[4]={0,5,3,1};//用来表示放i个3*3的盒子需要用多少个2*2的盒子填充,不明白的话那笔画一下
	while(scanf("%d %d %d %d %d %d",&n1,&n2,&n3,&n4,&n5,&n6)!=EOF){
		if(n1==0&&n2==0&&n3==0&&n4==0&&n5==0&&n6==0) break;
		int sum=n4+n5+n6;
		if(n5!=0){
			if(n5*11>=n1) n1=0;//用完了一定要化为0
			else{
				n1=n1-n5*11;
			}
		}
		if(n4!=0){
			if(n4*5>=n2){
				int leave=20*n4-4*n2;//这处的公式推导是:leave=36*n4-16*n4-4*n2
				n2=0;
				if(leave>=n1) n1=0;
				else{
					n1=n1-leave;
				}
			}
			else{
				n2=n2-n4*5;
			}
		}
		sum=sum+n3/4;
		n3=n3%4;
		if(n3>0){
			sum++;
			int s=36-9*n3-4*min(three[n3],n2);
			n2=n2-three[n3];
			if(n2<0) n2=0;
			if(n1<=s) n1=0;
			else n1=n1-s;
		}
		//再来判断2
		sum=sum+n2/9;
		n2=n2%9;
		if(n2>0){
			sum++;
			int s=36-4*n2;
			if(s>=n1) n1=0;
			else n1=n1-s;
		}
		sum=sum+n1/36;
		n1=n1%36;
		if(n1>0) sum++;
		printf("%d\n",sum);	
	}
	return 0;
}

你可能感兴趣的:(贪心算法,模拟)