贪心 —— POJ 1017 Packets

对应POJ题目:点击打开链接

Packets

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 46584   Accepted: 15752

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 

题意:

装箱问题

问题描述

一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个

型号,他们的长宽分别为 1*1, 2*2, 3*3, 4*4, 5*5, 6*6.  这些产品通常使用一个  6*6*h

的长方体包裹包装然后邮寄给客户。因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的

包裹数量。他们很需要有一个好的程序帮他们解决这个问题从而节省费用。现在这个程序由

你来设计。

输入数据

输入文件包括几行,每一行代表一个订单。每个订单里的一行包括六个整数,中间用空

格隔开,分别为 1*1 至6*6 这六种产品的数量。输入文件将以 6 个0 组成的一行结尾。

输出要求

除了输入的最后一行6 个0 以外,输入文件里每一行对应着输出文件的一行,每一行输

出一个整数代表对应的订单所需的最小包裹数。

输入样例

0 0 4 0 0 1

7 5 1 0 0 0

0 0 0 0 0 0

输出样例

2

1

思路:

高度无需考虑,只考虑面积就行。假设输入a1, a2, a3, a4, a5, a6

从面积最大的开始处理,可知,对于6*6大小的箱子,6*6的物品会霸占一个箱子;5*5的物品会余下11个能装1*1物品的空间;4*4的物品会余下20个1*1的空间,可以放2*2和1*1的物品;对于3*3的物品,所需的箱子数就是(a3 + 3) / 4(即是a3除以4向上取整),然后余数为0就刚好放完;为1就剩下27个1*1的空间,为2就剩下18个1*1的空间,为3就剩下9个1*1的空间;然后把2*2和1*1的物品尽可能多地往已用的箱子里面塞;多出的向上取整。

这题用到几个技巧可以缩短代码量,做完后观看网上的代码,好短~

本人代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main()
{
	int a1, a2, a3, a4, a5, a6;
	int i, ans;
	int b, r, c, p, all;
	//freopen("in.txt", "r", stdin);
	while(scanf("%d%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5, &a6) == 6)
	{
		if(0 == a1 + a2 + a3 + a4 + a5 + a6) break;
		ans = a6 + a5 + a4;

		c = 0;
		b = 5 * a4;
		ans += (a3 + 3) / 4;
		r = a3 % 4;
		if(1 == r) c = 5;
		else if(2 == r) c = 3;
		else if(3 == r) c = 1;
		b += c;

		p = 0;
		if(r) p = 36 - 9 * r;
		all = 11 * a5 + 20 * a4 + p;

		//if(b > a2){all -= 4 * (b - a2); a2 = 0;}
		if(b > a2){all -= 4 * a2; a2 = 0;}
		else{a2 -= b; all -= 4 * b;}

		if(all > a1) a1 = 0;
		else a1 -= all;

		ans += (a2 + 8) / 9;
		r = a2 % 9;
		all = 36 - 4 * r;
		if(r){
			if(all > a1) a1 = 0;
			else a1 -= all;
		}

		ans += (a1 + 35) / 36;

		printf("%d\n", ans);
	}
	return 0;
}

模仿网上代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main()
{
	//freopen("in.txt", "r", stdin);
	int a1, a2, a3, a4, a5, a6;
	int b, x, ans;
	int r[] = {0, 5, 3, 1};
	while(scanf("%d%d%d%d%d%d", &a1, &a2, &a3, &a4, &a5, &a6) == 6)
	{
		if(0 == a1 + a2 + a3 + a4 + a5 + a6) break;

		ans = a6 + a5 + a4 + (a3 + 3) / 4; //4*4,5*5,6*6都可以占一个箱子

		b = 5 * a4 + r[a3%4]; //计算目前为止所开的箱子能够装下2*2的物品的最大数量

		if(a2 > b) ans += (a2 - b + 8) / 9; //2*2的物品大于最大数量,应为剩下的开新的箱子

		x = 36 * ans - 36 * a6 - 25 * a5 - 16 * a4 - 9 * a3 - 4 * a2; //计算目前为止所开的箱子能够装下1*1的物品的最大数量

		if(a1 > x) ans += (a1 - x + 35) / 36; //1*1的物品大于最大数量,应为剩下的开新的箱子;

		printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-08-08 05:35:14

贪心 —— POJ 1017 Packets的相关文章

贪心/poj 1017 Packets

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 5 int main() 6 { 7 int a1,a2,a3,a4,a5,a6; 8 scanf("%d%d%d%d%d%d",&a1,&a2,&a3,&a4,&a5,&a6); 9 while ((a1+a2+a3+a4+a5+a6)!=0) 10 { 11 int ans=a6+a5

poj 1017 Packets 裸贪心

Packets Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43189   Accepted: 14550 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 alway

POJ 1017 Packets【贪心】

POJ 1017 题意: 一个工厂制造的产品形状都是长方体,它们的高度都是h,长和宽都相等,一共有六个型号,他们的长宽分别为 1*1, 2*2, 3*3, 4*4, 5*5, 6*6.  这些产品通常使用一个  6*6*h的长方体包裹包装然后邮寄给客户.因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的包裹数量.他们很需要有一个好的程序帮他们解决这个问题从而节省费用.现在这个程序由你来设计.输入数据输入文件包括几行,每一行代表一个订单.每个订单里的一行包括六个整数,中间用空格隔开,分别为 1

POJ 1017 Packets

这题可能是用贪心思想来写的,具体是不是我也不知道,自己对贪心也不是太清楚.之前好像写过,当时似乎没有写出来,不过,这次一下就有了思路.本来是想先装小的包裹,可是试了一下不行,有问题:于是改为先装大的包裹,如果有多余,再装小的,每次都这样,直到所有的包裹被装完为止.试了试,感觉没有什么问题. 代码写的可能有的乱,不过思路还是清楚的. #include<cstdio> #include<iostream> #include<cstring> #include<algo

POJ 1017 Packets(积累)

[题意简述]:这个是别人的博客,有清晰的题意描述,和解题思路,借助他的想法,可以很好的解决这个问题! [分析]:贪心?模拟?见代码 //216K 16Ms #include<iostream> using namespace std; int main() { int ans,a,b,c,d,e,f,Two,One;// 其中Two表示2*2的个数,同理One表示1*1的个数. int u[4] = {0,5,3,1}; while(1) { cin>>a>>b>

poj 1017(贪心)

[题目大意] 题目大意是这样的:某工厂生产几种产品,首先用packet包住,这些产品的高度都是h,底面积有1*1,2*2,3*3,4*4,5*5,6*6六种规格,下面我们要用高度为h,底面积为6*6的集装箱装这些货物,问怎样使所用集装箱数目最少? [解题思路] 我们首先必须先装底面积大的货物,并且对于面积为4*4,5*5,6*6的货物,每一件都需要一个独立的集装箱.对于底面积为3*3的货物,每四个需要一个集装箱. 那么我们可以得知对于装了底面积为3*3的货物的集装箱,其剩余可以装5,3,1个底面

贪心/POJ 3069 Saruman&#39;s Army

1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 int n,r,ans; 6 int a[1010]; 7 int cmax(int x,int y){return x>y?x:y;} 8 int main() 9 { 10 scanf("%d%d",&r,&n); 11 while (n!=-1 &

POJ 1017 Packets-装格子 (简单贪心)

http://poj.org/problem?id=1017 1.题意: 一个工厂制造的产品形状都是长方体盒子,它们的高度都是 h,长和宽都相等,一共有六个型号,分别为1*1, 2*2, 3*3, 4*4, 5*5, 6*6. 这些产品通常使用一个 6*6*h 的长方体箱子包装然后邮寄给客户.因为邮费很贵,所以工厂要想方设法的减小每个订单运送时的箱子数量BoxNum. 2.解题思路: 由于盒子和箱子的高均为h,因此只需考虑底面积的空间. 6*6的盒子,每个盒子独占一个箱子. 5*5的盒子,每个盒

poj 1017 贪心

题意:所有货物的高度一样,且其底面积只有六种,分别为1*1 2*2 3*3 4*4 5*5 6*6的,货物的个数依次为p1,p2,p3,p4,p5,p6, 包裹的高度与货物一样,且底面积就为6*6,然后求最少要多少个包裹包含以上所有货物 思路: 由于高度一样,所以忽略高度,只用讨论底面积. 分类讨论: 底面积为6*6的货物,需要1个包裹 底面积为5*5的货物,需要1个包裹,剩余空间用1*1货物填充 底面积为4*4的货物,需要1个包裹,剩余空间用2*2 / 1*1货物填充 底面积为3*3的货物,每