HDOJ1069 Monkey and Banana(DP,LIS)
2017-08-23 23:20
393 查看
题目:
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
题意分析:有若干种不同规格(长、宽、高)的
4000
砖块,每种砖块有无数个,可以自由选择以砖块的哪条边做长、宽或高,用这些砖块搭高塔,要求上面砖块的长宽必须严格小于下面砖块的长宽,问塔最高能有多高
我的做法是每读入一组长宽高,就把它分为三种不同的、长宽高定好的砖块,全部读完之后将这些砖块依次按照长宽高排序,从长宽最大的砖块开始依次求以该砖块为顶的塔最高能有多高:
对于第 i 块砖,我想前寻找第一块长宽均比它大的砖块 j ,进行优化:
dp [ i ] = max ( dp [ i ] , dp [ j ] + p [ i ] . z ) ; p [ i ] . z 就是第 i 块砖的高度。
dp 完之后再遍历一遍寻找最大的高度,当然,其实在 dp 的过程中就可以进行最大值的选取了。
解题:
一个长方体,可以有6种不同的摆法。
因为数据中 长方体种类最多30种,也就是说数组最大可以开到 30*6=180 完全可以
然后用dp[i]来存,到第i个木块,最高可以累多高。
当然,长方体先要以长度排序,长度相同则宽度小的在上。
以第一组测试数据为例:总共有6种变换:
10 20 30
10 30 20
20 10 30
20 30 10
30 10 20
30 20 10
那么对应的dp分别为:
dp[0] dp[1] dp[2] dp[3] dp[4] dp[5]
30 20 30 40 20 40
所以dp的最大值为40.
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
题意分析:有若干种不同规格(长、宽、高)的
4000
砖块,每种砖块有无数个,可以自由选择以砖块的哪条边做长、宽或高,用这些砖块搭高塔,要求上面砖块的长宽必须严格小于下面砖块的长宽,问塔最高能有多高
我的做法是每读入一组长宽高,就把它分为三种不同的、长宽高定好的砖块,全部读完之后将这些砖块依次按照长宽高排序,从长宽最大的砖块开始依次求以该砖块为顶的塔最高能有多高:
对于第 i 块砖,我想前寻找第一块长宽均比它大的砖块 j ,进行优化:
dp [ i ] = max ( dp [ i ] , dp [ j ] + p [ i ] . z ) ; p [ i ] . z 就是第 i 块砖的高度。
dp 完之后再遍历一遍寻找最大的高度,当然,其实在 dp 的过程中就可以进行最大值的选取了。
解题:
一个长方体,可以有6种不同的摆法。
因为数据中 长方体种类最多30种,也就是说数组最大可以开到 30*6=180 完全可以
然后用dp[i]来存,到第i个木块,最高可以累多高。
当然,长方体先要以长度排序,长度相同则宽度小的在上。
以第一组测试数据为例:总共有6种变换:
10 20 30
10 30 20
20 10 30
20 30 10
30 10 20
30 20 10
那么对应的dp分别为:
dp[0] dp[1] dp[2] dp[3] dp[4] dp[5]
30 20 30 40 20 40
所以dp的最大值为40.
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> using namespace std; struct node{ int l,w,h; }cd[181]; /*dp[i]表示到第i个木块,最高可以到多高 dp[i]=max(dp[i],dp[j]+cd[i].h)其中cd[i].h就是第i块的高度 并且0<=j<=i */ int dp[181]; int max(int a,int b) {return a>b?a:b;} bool cmp(node a,node b){ if(a.l==b.l) return a.w<b.w; return a.l<b.l; } int main(){ int i,j,n,len,num=1; int z1,z2,z3; while(cin>>n &&n){ len=0; //每组变换为6种长方体 for(i=0;i<n;++i) { cin>>z1>>z2>>z3; cd[len].l=z1,cd[len].w=z2,cd[len++].h=z3; cd[len].l=z1,cd[len].w=z3,cd[len++].h=z2; cd[len].l=z2,cd[len].w=z1,cd[len++].h=z3; cd[len].l=z2,cd[len].w=z3,cd[len++].h=z1; cd[len].l=z3,cd[len].w=z1,cd[len++].h=z2; cd[len].l=z3,cd[len].w=z2,cd[len++].h=z1; } sort(cd,cd+len,cmp); dp[0]=cd[0].h; //构造dp数组 int max_h; for(i=1;i<len;i++){ max_h=0; for(j=0;j<i;j++){ if(cd[j].l<cd[i].l &&cd[j].w<cd[i].w) max_h=max_h>dp[j]?max_h:dp[j]; } dp[i]=cd[i].h+max_h; } int ans=0; for(i=1;i<len;i++){ ans=max(ans,dp[i]); } cout<<"Case "<<num++<<": maximum height = "<<ans<<endl; } return 0; }
相关文章推荐
- HDOJ 1069 Monkey and Banana(DP)
- hdoj1069 Monkey and Banana(DP)
- HDU 1069 Monkey and Banana DP LIS
- HDU 1069 Monkey and Banana DP LIS
- HDU 1069 Monkey and Banana DP LIS变形题
- 【DP|LIS】HDU-1069 Monkey and Banana
- hdu 1069 Monkey and Banana 再来一波DP啦~~LIS的变形,会最大上升子序列就可以A啦~
- hdoj1069_Monkey and Banana(dp)
- 【HDU 1069 】Monkey and Banana 【 求LIS的思想 DP 】
- HDOJ1069-Monkey and Banana(DP)
- hdoj--1069 Monkey and Banana(dp)
- HDOJ1069 Monkey and Banana【dp】
- HDOJ 1069 Monkey and Banana 【DP】
- hdoj 1069 Monkey and Banana【dp】
- hdoj 1069 Monkey and Banana 【DP】
- [HDOJ1069]Monkey and Banana(DP)
- HDOJ 1069 Monkey and Banana(LIS)
- HDU 1069 Monkey and Banana(dp)
- HDOJ 1069 Monkey and Banana
- hdu1069 Monkey and Banana(二维LIS)