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;
}



ZOJ 1093 Monkey and banana

#include #include #define MAXN 30#define MAX(a, b) a>b?a:b#define MIN(a, b) a>b?b:ausing namespace s...
  • yllfever
  • yllfever
  • 2008年06月24日 12:54
  • 468

zoj 1093 Monkey and Banana

DP 第一次做竟然连题目都没看懂,最后看了别人的代码,最长不下降子序列一点也不熟练。 砖头无穷多但每个类型的长方体有三种变换方法。 先求出三种变换形态,按长宽大小排序。最后求最长不下降子...
  • regnjka
  • regnjka
  • 2013年10月27日 17:54
  • 305

ZOJ - 1093 Monkey and Banana

Monkey and Banana Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %lld & ...
  • acm_1361677193
  • acm_1361677193
  • 2014年08月09日 15:54
  • 266

Zoj 1093 Monkey and Banana

Monkey and Banana Time Limit: 2 Seconds      Memory Limit: 65536 KB A group of researchers ...
  • Magic____
  • Magic____
  • 2013年07月17日 09:51
  • 611

ZOJ 1093 Monkey and Banana

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=93
  • u013615904
  • u013615904
  • 2014年11月25日 14:56
  • 358

HDU1069 Monkey and Banana (动态规划)

Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other...
  • i1020
  • i1020
  • 2017年02月05日 17:21
  • 168

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

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

ZOJ 1093 Monkey Banana(LCS)

把每个箱子的x,y,z所有排列都列出来,然后按照x,y排序,最后就是最长递增子序列的问题了. #include #include #include #include using name...
  • zxjcarrot
  • zxjcarrot
  • 2013年05月03日 10:04
  • 293

zoj 1093 dp Monkey and Banana

   原来老早看了..觉得麻烦后来发现是自己想麻烦了..没有我想的那种情况..原来想的可能存在一边相等,另一边比底下放好的小的情况...这样一层一层往上加箱子还可能存在就根楼梯那种情况..还要保存那条...
  • zjy3186511
  • zjy3186511
  • 2011年04月28日 22:41
  • 527

ZOJ 1093—Monkey and Banana 解题报告

题目大意:
  • u013967323
  • u013967323
  • 2014年04月04日 20:23
  • 556
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Monkey and Banana (zoj 1093 动态规划)
举报原因:
原因补充:

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