Monkey and Banana (zoj 1093 动态规划)

原创 2015年11月21日 15:51:30

Monkey and Banana

Time Limit: 2 Seconds      Memory Limit: 65536 KB

A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food.

The researchers have n types of blocks, and an unlimited supply ofblocks of each type. Each type-i block was a rectangular solid withlinear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the otherdimension was the height.

They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placedon top of another block as long as the two base dimensions of theupper block were both strictly smaller than the corresponding basedimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized basescouldn't be stacked.

Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks.

Input Specification

The input file will contain one or more test cases. The first line of each test case containsan integer n,
representing the number of different blocks in the following data set.The maximum value for n is 30.
Each of the next n lines contains three integersrepresenting the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.

Output Specification

For each test case, print one line containing the case number(they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format"Case case: maximum height = height"

Sample Input

1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0

Sample Output

Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342

题意:有n种长方体,数量不限,把它们堆起来,下面的底要严格大于上面的,问最高能堆多高。

思路:每种长方体可以预处理成三种长方体,然后按照底面从大到小排个序,然后的操作感觉就有点想LIS了,dp[i]表示到第i个长方体的最高高度。

代码:

/*************************************************************************
    > File Name: zoj1093_堆箱子.cpp
    > Author: wust_lyf
    > Mail: 2206478849@qq.com
    > Created Time: 2015年11月21日 星期六 12时30分40秒
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define eps 1e-6
#define DBG printf("Hi\n")
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 105;
const int MAXN = 2005;

struct Box
{
	int x,y,z;
}box[maxn];

int n;
int N;
int dp[maxn];

int cmp(Box a,Box b)
{
	if (a.x==b.x)
		return a.y>b.y;
	return a.x>b.x;
}

int main()
{
	int i,j;
	int cas=0;
	while (scanf("%d",&n),n)
	{
		N=1;
		for (i=0;i<n;i++)
		{
			scanf("%d %d %d",&box[N].x,&box[N].y,&box[N].z);
			if (box[N].x>box[N].y) swap(box[N].x,box[N].y);
			N++;
			box[N].x=box[N-1].x; box[N].y=box[N-1].z; box[N].z=box[N-1].y;
			if (box[N].x>box[N].y) swap(box[N].x,box[N].y);
			N++;
			box[N].x=box[N-2].y; box[N].y=box[N-2].z; box[N].z=box[N-2].x;
			if (box[N].x>box[N].y) swap(box[N].x,box[N].y);
			N++;
		}
		N--;
		sort(box+1,box+N+1,cmp);
		memset(dp,0,sizeof(dp));
		int ans=0;
		for (i=1;i<=N;i++)
		{
			int Max=0;
			dp[i]=box[i].z;
			for (j=1;j<i;j++)
			{
				if (box[i].x<box[j].x&&box[i].y<box[j].y&&dp[j]>Max)
				{
					Max=dp[j];
				}
			}
			dp[i]=max(dp[i],Max+box[i].z);
			ans=max(ans,dp[i]);
		}
		printf("Case %d: maximum height = %d\n",++cas,ans);
	}
    return 0;
}



相关文章推荐

hdu1069 Monkey and Banana 一个简单的动态规划

题目链接 前一天睡前看了一道题,睡觉的时候想了想,一大早(误)起来写了一下交了然后就AC了。 嘿!太难得了!(…… 感觉是思路很简单的一个动态规划,然后也没有做任何优化,连双关键字排序也只是写的选...

HDU 1069.Monkey and Banana【动态规划(DP)】【8月15】

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...

HDU Monkey and Banana 动态规划

动态规划专题 最大上升子序列问题 dp方程 预处理

动态规划入门——Monkey and Banana

转载请注明出处:http://blog.csdn.net/a1dark 分析:题意是堆箱子、要求上面的箱子的长和宽都小于下面箱子的长和宽、由于箱子可以无限放、所以一种箱子有6种情况、然后我们按照其中...

hdu 1069 Monkey and Banana(动态规划)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS (...

杭电ACM1069——Monkey and Banana~~动态规划

题目的意思是:给你N个长方体的长a宽b高c,每一个型号的长方体可以用无限多个。用这些长方体来堆一个高度最高的塔,每一个长方体的长宽大于上面一个长方体的长宽。 一开始是这么想的,一个长方体有3个不同的面...

ACM--steps-3.2.5--Monkey and Banana//动态规划的最大子列和

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot...

HDU 1069(Monkey and Banana)动态规划

这题看上去和最长递增子序列好像,需要长和宽都递增,然后求子序列和最大。 一块block有6种摆法:高度3种 X 长和宽互换2种,枚举这六种,以长从大到小排序,然后求递增序列和最大,转移方程就是把原来...

hdu-1069-Monkey and Banana(动态规划)

Problem DescriptionA group of researchers are designing an experiment to test the IQ of a monkey. Th...

Monkey and Banana(DAG上的动态规划问题)

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Monkey and Banana (zoj 1093 动态规划)
举报原因:
原因补充:

(最多只允许输入30个字)