题目链接
今天做的第二题,感觉比较经典,详细记录一下吧!
Monkey and Banana
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25500 Accepted Submission(s): 13710
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
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种长方体。
然后,将一个香蕉挂在屋顶,让猩猩通过 叠长方体来够到香蕉。
现在给你N种长方体,可重复使用,计算,最高能堆多高。
要求位于上面的长方体的长要大于(注意不是大于等于)下面长方体的长,上面长方体的宽大于下面长方体的宽。
思路:(写得还是比较迷,先这样吧)
这是一道经典的DP问题(96年,比我还大两岁OTZ)
想要做对队这道题,首先要看懂题吧,奈何英语六级都只是飘过,只能用翻译,对比百度翻译和谷歌翻译,发现还是谷歌翻译的准哈哈哈哈哈哈哈哈哈哈哈哈。
其次呢,要真正理解DP思想!
动态规划算法与分治法类似,其基本思想也是将待求解问题分解成若干个子问题,先求解子问题,然后从这些子问题的解得到原问题的解。
积木是block,我用的lego哈哈哈
状态转移方程:dp[i]=max(dp[i-1])+lego[i].h
dp[i]表示以第i块积木为底层能达到的最大高度(前提是,积木长、宽从小到大排好序,下图用底长表示,文字不好描述,降为二维形式演示)
例如下图以积木3为底层,则要考虑在其上面加上以1、2为底层的情况(这两种情况都满足条件),再选择最大的一个
这里的思想和最大上升子序列很像,可以参照思考!
完整代码:
#include <iostream>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;struct lego{int x,y;int h;}lg[100];bool cmp(lego a,lego b){if(a.x==b.x)return a.y<b.y;elsereturn a.x<b.x;}int n;int tmp;int dp[100];int main(){int i=1;while(cin>>n){if(!n)break;memset(dp,0,sizeof(dp));for(int i=0;i<n;i++){int L[4];cin>>L[1]>>L[2]>>L[3];sort(L+1,L+4);//从小到大排序//以下操作 示例//有6 8 10积木 排序 6 8 10//则有以下三种摆放//10 8 6//10 6 8//8 6 10//第一列为长,第二列为宽,第三列为高lg[3*i+1].x=L[3];lg[3*i+1].y=L[2];lg[3*i+1].h=L[1];lg[3*i+2].x=L[3];lg[3*i+2].y=L[1];lg[3*i+2].h=L[2];lg[3*i+3].x=L[2];lg[3*i+3].y=L[1];lg[3*i+3].h=L[3];}sort(lg+1,lg+n*3+1,cmp);//为3*n中积木总体排序(长、宽从小到大)//for(int i=1;i<=n*3+1;i++)//排序输出测试//cout<<lg[i].x<<" "<<lg[i].y<<" "<<lg[i].h<<endl;for(int i=1;i<=3*n;i++){int tmp=0;for(int j=1;j<i;j++){if(lg[j].x<lg[i].x&&lg[j].y<lg[i].y)tmp=max(tmp,dp[j]);//记录下以第i块前面各块为底的最大值}dp[i]=lg[i].h+tmp;//以第i块为底的值}int mx=0;for(int i=1;i<=3*n;i++){//从dp中找最大值,不是最后一个dp的原因是://最后一块不一定就满足情况if(dp[i]>mx)mx=dp[i];}cout<<"Case "<<i++<<": maximum height = "<<mx<<endl;}return 0;}