【HDU 1069】Monkey and Banana(dp+sort结构体排序)

来源:互联网 发布:windows server 2012 编辑:程序博客网 时间:2024/06/05 19:47


点击打开题目


Monkey and Banana

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15927    Accepted Submission(s): 8444


Problem 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
110 20 3026 8 105 5 571 1 12 2 23 3 34 4 45 5 56 6 67 7 7531 41 5926 53 5897 93 2384 62 6433 83 270
 

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

Source
University of Ulm Local Contest 1996 

题意:

一组研究人员正在设计一项实验,以测试猴子的智商。他们将挂香蕉在建筑物的屋顶,同时,提供一些砖块给这些猴子。如果猴子足够聪明,它应当能够通过合理的放置一些砖块建立一个塔,并爬上去吃他们最喜欢的香蕉。
 
研究人员有n种类型的砖块,每种类型的砖块都有无限个。第i块砖块的长宽高分别用xi,yi,zi来表示。 同时,由于砖块是可以旋转的,每个砖块的3条边可以组成6种不同的长宽高。
 
在构建塔时,当且仅当A砖块的长和宽都分别小于B砖块的长和宽时,A砖块才能放到B砖块的上面,因为必须留有一些空间让猴子来踩。
 
你的任务是编写一个程序,计算猴子们最高可以堆出的砖块们的高度。
Input
输入文件包含多组测试数据。
每个测试用例的第一行包含一个整数n,代表不同种类的砖块数目。n<=30.
接下来n行,每行3个数,分别表示砖块的长宽高。
当n= 0的时候,无需输出任何答案,测试结束。
Output
对于每组测试数据,输出最大高度。格式:Case 第几组数据: maximum height = 最大高度


题解:


因为一块砖有长宽高3个不同的边,所以每一组数据都有3个不同的高。
保存砖块 的长宽高,按照砖块长宽进行降序排列,因为要使长宽大的尽量拍在下面,
这样堆积其来的才高。所以长从大到小进行排序。
一种砖虽然有无数个,最多用两个,因为从小往上看是递减的,下面的只能大于上面,连等于都不行,长宽高三个性质,全排列有六种,将所有砖块的这六种排列排序
,长从大到小,如果长相等,就按宽从大到小,这样排序之后,就变成了求最大单调子序列了。

分析:dp[i]=max( for(j=i-1;j>=0;j--)    dp[j]+block[i].h  ,dp[i] )
输入一组数组要存入6个数据,分别对应6中不同的摆放方式,(其实也可以只要存三组同时保证宽小于长)->排序->再用状态转移方程:
dp[i]  表示以第i个砖作为底部可以累加的最大高度
dp[i]=max( for(j=i-1;j>=0;j--)    dp[j]+block[i].h  ,dp[i] )

代码:
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int MAX=1e3+10;int high[MAX];struct Block{int x,y,z;}block[MAX];bool cmp(Block a,Block b){if(a.x==b.x)return a.y<b.y;elsereturn a.x<b.x;}int main(){int n;int t=1;while(cin>>n,n){int x,y,z;int k=0;for(int i=0;i<n;i++){cin>>x>>y>>z;block[k].x=x,block[k].y=y,block[k].z=z,k++;block[k].x=x,block[k].y=z,block[k].z=y,k++;block[k].x=y,block[k].y=x,block[k].z=z,k++;block[k].x=y,block[k].y=z,block[k].z=x,k++;block[k].x=z,block[k].y=x,block[k].z=y,k++;block[k].x=z,block[k].y=y,block[k].z=x,k++;}sort(block,block+k,cmp);for(int i=0;i<k;i++){high[i]=block[i].z;} int ans=0;for(int i=0;i<k;i++){for(int j=0;j<=i;j++){if(block[j].x<block[i].x && block[j].y<block[i].y && high[i]<high[j]+block[i].z){//判断条件high[i]=high[j]+block[i].z;//核心dp思想ans=max(high[i],ans);}}}cout<<"Case "<<t++<<": maximum height = ";cout<<ans<<endl;}return 0;}