题目:
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

md,题目那么长,昨天读了半天才懂,然后调一晚上才a过去。

分析:

其实这题和我之前做的一个矩形嵌套十分类似。但是有两个很大的区别就是:
1.这里是求高度最高,而矩形嵌套是求矩形最多的个数。
2.这里的长方体每一个型号是可以选多个的(看你怎么摆了,总之一个长方体可有6中方式摆放)情况较多,而矩形嵌套只能给定多少个矩形就多少个,并且只有长和宽,简单得不行。

依然是DP的思想,通过转换成最长上升子序列的问题。
令长方体的三个棱长为x,y,z,那么有多少种方式摆放呢?

      长    宽    高1 x     y     z2 y     x     z3 x     z     y4 z     x     y5 y     z     x6 z     y     x

所以有六种,也就是说每一个长方体最多可拿6个,再多拿,怎么摆都会和之前拿的重复。那么我们就用一个数组保存3个输入的值,则只需枚举长和宽即可,高就是数组中的另外一个数。然后放到一个新的数组里(这个数组用定义的长方体结构体定义),最后就和矩形嵌套的方法一样了,注意要先sort一下,重写一下cmp即可,这样高度才能达最高。

细节:这里长和宽可以颠倒,即全排列,因为我之前一直用矩形嵌套的思想,只要长大于宽就行,所以我当时确定一个长方体最多拿3个,可这是三维的,所以我傻b了,就因为这个调了我一个小时QAQ。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 200;
int dp[maxn];
int n;
/*声明长方体结构体,为了保存输入数据*/
struct Rec{int r[3];
}rec[30+5];
/*声明新长方体结构体,为了摆放方式*/
struct Rec_n{int x,y;int height;
}rec_n[maxn];
/*结构体类型数组排序*/
int cmp(const Rec_n &a, const Rec_n &b)
{if(a.x<b.x) return 1;else if(a.x==b.x&&a.y<b.y) return 1;else return 0;
}
/*DP*/
int solve()
{int res = 0;for(int i=0; i<n*6; i++){dp[i] = rec_n[i].height;for(int j=0; j<i; j++){if((rec_n[i].x > rec_n[j].x) && (rec_n[i].y > rec_n[j].y)){dp[i] = max(dp[i], dp[j] + rec_n[i].height);}}res = max(res, dp[i]);}return res;
}int main()
{int ca = 1;while(scanf("%d",&n)==1&&n){for(int i=0; i<n; i++){scanf("%d%d%d",&rec[i].r[0], &rec[i].r[1], &rec[i].r[2]);}int k=0;for(int i=0; i<n; i++){Rec_n r1,r2,r3,r4,r5,r6;//6种摆放方式r1.x = rec[i].r[0]; r1.y = rec[i].r[1]; r1.height = rec[i].r[2];rec_n[k++] = r1;r2.x = rec[i].r[0]; r2.y = rec[i].r[2]; r2.height = rec[i].r[1];rec_n[k++] = r2;r3.x = rec[i].r[1]; r3.y = rec[i].r[2]; r3.height = rec[i].r[0];rec_n[k++] = r3;r4.x = rec[i].r[1]; r4.y = rec[i].r[0]; r4.height = rec[i].r[2];rec_n[k++] = r4;r5.x = rec[i].r[2]; r5.y = rec[i].r[0]; r5.height = rec[i].r[1];rec_n[k++] = r5;r6.x = rec[i].r[2]; r6.y = rec[i].r[1]; r6.height = rec[i].r[0];rec_n[k++] = r6;}sort(rec_n,rec_n+n*6,cmp);printf("Case %d: maximum height = %d\n",ca++,solve());}return 0;
}//AC

三维最长上升子序列问题——HDU 1069 Monkey and Banana相关推荐

  1. HDU 1069 Monkey and Banana 最长上升子序列进阶(动态规划)

    HDU 1069(动态规划) Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  2. HDU 1069 Monkey and Banana(简单图解,经典DP,最大上升子序列变形)

    题目链接 今天做的第二题,感觉比较经典,详细记录一下吧! Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  3. hdu(1069)——Monkey and Banana(LIS变形)

    题意: 如今给你n个石块,然后它由坐标来表示(x,y,z).可是它能够有不同的方法,也就是说它的三个坐标能够轮换着来的. 石块的数量不限,可是每次都必须保持上底面的长和宽严格递减,然后问你用这些石块所 ...

  4. hdu 1069 Monkey and Banana (LIS)

    Problem - 1069 随便找到的一道题目. 题意是给出一些的长方体,长方体可以用任意次数,可以任意翻转.如果一个长方体可以叠在另一个长方体上,条件是这个长方体的长和宽严格小于另一个长方体的长和 ...

  5. hdu 1069 Monkey and Banana 【动态规划】

    题目 题意:研究人员要测试猴子的IQ,将香蕉挂到一定高度,给猴子一些不同大小的箱子,箱子数量不限,让猩猩通过叠长方体来够到香蕉. 现在给你N种长方体, 要求:位于上面的长方体的长和宽  要小于  下面 ...

  6. hdu 1069 Monkey and Banana dp

    题意:给你n种个长方体,告诉你每种长方体的长,宽,高,并且每种长方体个数不限 问:把它们堆起来的最大高度 限制条件:上面一个长方体a的底面积,必须比它下面一个长方体b的底面积大,而且,a的底面的长宽也 ...

  7. HDU 1069 Monkey and Banana

    传送门 #include<iostream> #include<algorithm> #include<map> #include<set> #incl ...

  8. HDUOJ 1069 Monkey and Banana

    HDUOJ 1069 Monkey and Banana 题目链接 Problem Description A group of researchers are designing an experi ...

  9. 最长上升子序列模板 hdu 1087 Super Jumping! Jumping! Jumping!

    Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular i ...

最新文章

  1. Jsoup 替换文本中所有的img src属性
  2. cordova 学习笔记
  3. jetson nano 摄像头购买
  4. Java用户自定义函数
  5. C++基础与面向对象
  6. 直播预告 | 后广告时代数据助力融合媒体用户收入增长
  7. 2014广州赛区比赛总结
  8. 什么是python函数_Python之什么是函数
  9. 备忘录怎么用红笔标注_错题本的正确打开方法,你用对了吗?
  10. 密码学常用的算法填充模式_密码学的操作模式
  11. STL之partial_sum
  12. Android异步加载全解析之引入二级缓存
  13. 【高德地图API】如何打造十月妈咪品牌地图?
  14. 我靠! 算你运气好 a joke
  15. layui 自定义排序_layui使用心得
  16. pip或者python安装jpype总是报错----Boilerpipe使用
  17. M1芯片MAC使用VMware Fusion安装Windows 11
  18. 常见的运营商网络安全问题
  19. More Grounded Image Captioning by Distilling Image-Text Matching Model
  20. CCC3.0 蓝牙OOB配对

热门文章

  1. 扫雷游戏的设计与实现
  2. LeetCode473. 火柴拼正方形
  3. python常用函数汇总
  4. Git 基础之凌波微步
  5. gitlab flake8错误
  6. 华为云服务器mysql数据恢复案例
  7. 蒲公英的约定在线试听,蒲公英的约定歌词,下载
  8. 好的应用恰恰用对了场景
  9. python字符串的内置函数_python——字符串内置函数
  10. 双色球手机版 旋转矩阵手机版