[kuangbin带你飞]专题十二 基础DP1 C - Monkey and Banana HDU - 1069

题目描述

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 of blocks of each type. Each type-i block was a rectangular solid with linear 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 other dimension 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 placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions 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 bases couldn'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
The input file will contain one or more test cases. The first line of each test case contains an 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 integers representing the values xi, yi and zi.
Input is terminated by a value of zero (0) for n.
Output
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

题目大意如下:给定不同种类的木箱子,箱子的数量是无限的。把箱子一个一个叠起来,箱子的任何面都可以做底面,下面箱子的长宽必须严格大于上面箱子的长宽,也就是说上下两箱子长或宽相同的情况是不允许的。求箱子可以叠的最高的高度。

解题思路

这道题放到了动态规划的类别里,但是刚看到题目,我第一个想法是用广度优先搜索来做,用每种箱子的三种面当做搜素方向(长方体有六个面,但两两对称,因此有三种面),比如说有3种箱子,那搜索方向就是3*3。搜索的起始是每种箱子的每种面作为第一个节点。当队列中节点为空时,结束搜索,遍历的时候用一个变量记录最高的高度。但是这种方法交上去T了,搜索的方向太多了,超时很正常。
参考了其他博客,了解到这道题其实是最长上升子序列的变形。最长上升子序列的概念其实很简单,从这篇博客.中拿一个例子来做解释。

举个栗子:给你一个序列为(1,5 ,2,6,9,10,3,15),那么它的最长上升子序列为:(1,2,6,9,10,15)

最长上升子序列的题目方法有很多,最简单的方法时间复杂度是 O ( n 2 ) O(n^{2}) O(n2),使用二分+贪心法或者树状数组优化,时间复杂度可达到 O ( n l o g ( n ) ) O(nlog(n)) O(nlog(n)),笔者今天只学习了最简单的方法,更优化的方法大家可以移步这篇博客。这道题中时间复杂度达到 O ( n 2 ) O(n^{2}) O(n2)就能AC了。
最长子序列的问题是典型的动态规划问题。动态规划问题的核心就是找状态转移方程。我们维护一个状态数组d[i],这个状态数组表示以s[i]结尾的子序列最长的长度。使用下面方程来更新这个状态数组。
d [ i ] = m a x ( d [ j ] + 1 , d [ i ] ) d[i] = max(d[j]+1,d[i]) d[i]=max(d[j]+1,d[i])
其中j<i其s[j]<s[i]。这个方程告诉我们,d[i]的状态依赖于他前面比他小的数字的状态,我们只需要找到那些在他前面比他小的值的d[j],然后把最大的d[j]拿出来+1,就得到了d[i]的值。将所有d[i]求出后,取最大的那个值就是整个序列的最长上升子序列的长度。
回到这道题中,我们没有序列,只有箱子的长宽高的数据。因此,第一步要构造一个序列,序列的元素就是箱子的长宽高,我们可以用结构体来存储。要注意,因为箱子是可以随意摆放的,因此,我们这里一种箱子就会存三个结构体。然后,将得到的结构体排序,排序的依据是先比长,长度长的在前,当长度相同的时候,比宽度,宽度长的在前面。排好序后,我们就得到了一个序列,然后对这个序列求最长上升子序列,不过这里不同点在于:1、我们维护的d并不是真的序列的长度,而是箱子叠加到达的高度。2、序列中两个元素需要比较长和宽,必须长宽都严格大于才能算一个元素大于另一个元素。

AC 代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct node
{
	int x,y,z;
}nd[100];
bool cmp(node a,node b){
    if(a.x==b.x)
        return a.y>b.y;
    else
        return a.x>b.x;
}
//int x_array[30*3+5],y_array[30*3+5],z_array[30*3+5];
int n;
int dp[100];
int main()
{
	int i,j;
	int x,y,z;
	int case_count = 1;
	while(scanf("%d",&n)&&n!=0)
	{
		int num = 0;
		memset(dp,0,sizeof(dp));
		for(i = 0;i<n;i++) 
		{
			scanf("%d%d%d",&x,&y,&z);
			nd[num].x = max(x,y),nd[num].y = min(x,y),nd[num].z = z;
			num++;
			nd[num].x = max(x,z),nd[num].y = min(x,z),nd[num].z = y;
			num++;
			nd[num].x = max(y,z),nd[num].y = min(y,z),nd[num].z = x;
			num++;
		}
		sort(nd,nd+n*3,cmp);
//		for(i = 0;i<n*3;i++)
//		{
//			cout<<nd[i].x<<" "<<nd[i].y<<" "<<nd[i].z<<endl;
//		}
		dp[0] = nd[0].z;
		int mx = dp[0];
		for(i = 1;i<n*3;i++)
		{
			dp[i] = nd[i].z;
			for(j = 0;j<i;j++)
			{
				if(nd[j].x>nd[i].x&&nd[j].y>nd[i].y)
				{
//					cout<<"**"<<dp[j]<<" "<<nd[i].z<<" "<<dp[i]<<endl;
					dp[i] = max(dp[j]+nd[i].z,dp[i]);
				}
			}
//			cout<<dp[i]<<" "<<mx<<endl;
			mx = max(mx,dp[i]);
		}
//		int max_height = BFS();
		printf("Case %d: maximum height = %d\n",case_count,mx);
		case_count++;
	}
}

顺便把T了的广度优先搜索的代码也贴上

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
int x_array[31],y_array[31],z_array[31];
int n;
struct node
{
	int x,y,height;
};
int BFS()
{
	int max_height = 0;
	int i,j;
	int x,y,height;
	queue<node> que;
	node new_node,add_node;
	for(i = 0;i<n;i++)
	{
		for(j = 0;j<3;j++)
		{
			if(j == 0)
			{
				x = x_array[i];
				y = y_array[i];
				height = z_array[i];
			}
			if(j == 1)
			{
				x = z_array[i];
				y = y_array[i];
				height = x_array[i];
			}
			if(j == 2)
			{
				x = x_array[i];
				y = z_array[i];
				height = y_array[i];
			}
			add_node.x = x;
			add_node.y = y;
			add_node.height = height;
			que.push(add_node);
		}
	}
	while(!que.empty())
	{
    	new_node = que.front();
		que.pop();
		if(new_node.height>max_height) max_height = new_node.height;
	   for(i = 0;i<n;i++)
	   {
			for(j = 0;j<3;j++)
			{
				if(j == 0)
				{
					x = x_array[i];
					y = y_array[i];
					height = z_array[i];
				}
				if(j == 1)
				{
					x = z_array[i];
					y = y_array[i];
					height = x_array[i];
				}
				if(j == 2)
				{
					x = x_array[i];
					y = z_array[i];
					height = y_array[i];
				}
				if(max(x,y)>max(new_node.x,new_node.y)&&min(x,y)>min(new_node.x,new_node.y))
				{
					
				    add_node.x = x;
				    add_node.y = y;
//				    cout<<add_node.height<<" "<<height<<endl;
				    add_node.height = new_node.height+height;
//				    cout<<"**"<<endl;
				    
//				    cout<<add_node.height<<endl;
				    que.push(add_node);
						
				}

			}
     	}	
	}
	return max_height;
}
int main()
{
	
	int i,j;
	int case_count = 1;
	while(scanf("%d",&n)&&n!=0)
	{
		for(i = 0;i<n;i++) scanf("%d%d%d",&x_array[i],&y_array[i],&z_array[i]);
		int max_height = BFS();
		printf("Case %d: maximum height = %d\n",
		case_count,max_height);
		case_count++;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值