hdu -1069 Monkey and Banana【贪心+dp】

C - Monkey and Banana
Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u
Submit Status

Description

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

贪心+动态规划


定义一个列表table 存可能取的长方体的摆放方式

先对x,y,z进行由小到大排序得到 a[0],a[1],a[2]。每个长方体有有 (长,宽,高):(a[2],a[1],a[0]),(a[2],a[0],a[1]),(a[1],a[0],a[2])三种摆放方式

可以证明:按照规则堆砌,无论怎么样都只能三选其二 。这样与题目中长方体最多取两个不矛盾,就可以把三个摆放方式都加入到table中。

为了多的堆砌。对table按买面积由小到大进行排序。

再求table中的单调 " 递增 " 最长子序列就是解。

反过来想构造一个序列,它的单调递增子序列最长,那么贪心策略就是使它本身尽可能的满足单调递增。

#include <stdio.h>
#include <vector>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#define MAX_SIZE 10000
#define find_max(a,b) a>b?a:b;
using namespace std;
struct node{
	int l,w,h;
	bool operator < (const node &p) const{
		return l*w<p.l*p.w;
	}
	bool operator >(const node &p) const{
		return (l>p.l)&&(w>p.w);
	}
};
vector<node> ln;
int dp[3*MAX_SIZE];
int main()
{
	int n;
	int count=1;
	while(~scanf("%d",&n))
	{
		if(n==0)
			break;
		ln.clear();
		int a[3];
		for(int i=0;i<n;++i)
		{
			scanf("%d%d%d",a,a+1,a+2);
			sort(a,a+3);
			ln.push_back((node){a[2],a[1],a[0]});
			ln.push_back((node){a[2],a[0],a[1]});
			ln.push_back((node){a[1],a[0],a[2]});
		}
		sort(ln.begin(),ln.end());
		dp[0]=ln[0].h;
		int ans=ln[0].h;
		for(int i=1;i<ln.size();++i)
		{
			dp[i]=ln[i].h;
			for(int j=i-1;j>=0;--j)
				if(ln[i]>ln[j])
					dp[i]=find_max(dp[i],dp[j]+ln[i].h);
			ans=find_max(ans,dp[i]);
		}
		printf("Case %d: maximum height = %d\n",count,ans);
		++count;
	}
	return 0;
}


你可能感兴趣的:(hdu -1069 Monkey and Banana【贪心+dp】)