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

题解:对于箱子的摆放方式进行枚举,每次6种,计算他们的面积,然后按照面积由小到大排序,然后在dp的时候需要找i之前最优的,条件是i的长和宽都必须严格大于前面的,然后遍历一次找出最大高度即可(滚去写数电!!!)

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> using namespace std; struct node
{
int a;
int b;
int c;
int s;
}r[20005];
bool cmp(node x,node y)
{ return x.s<y.s; }
int main()
{
int cnt=1,n,k,l,w,h,dp[20005],temp;
while(scanf("%d",&n)!=EOF&&n)
{ memset(dp,0,sizeof(dp));
k=1;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&l,&w,&h);
r[k].a=l;r[k].b=w;r[k].c=h;r[k].s=r[k].a*r[k].b;
k++;
r[k].a=w;r[k].b=l;r[k].c=h;r[k].s=r[k].a*r[k].b;
k++;
r[k].a=h;r[k].b=l;r[k].c=w;r[k].s=r[k].a*r[k].b;
k++;
r[k].a=h;r[k].b=w;r[k].c=l;r[k].s=r[k].a*r[k].b;
k++;
r[k].a=l;r[k].b=h;r[k].c=w;r[k].s=r[k].a*r[k].b;
k++;
r[k].a=w;r[k].b=h;r[k].c=l;r[k].s=r[k].a*r[k].b;
k++;
}
sort(r+1,r+k,cmp);
dp[1]=r[1].c;
for(int i=1;i<k;i++)
{
temp=0;
for(int j=1;j<i;j++)
if(r[i].a>r[j].a&&r[i].b>r[j].b)
temp=max(temp,dp[j]);
dp[i]=temp+r[i].c;
}
int max=0;
for(int i=1;i<k;i++)
if(max<dp[i])
max=dp[i];
printf("Case %d: maximum height = %d\n",cnt++,max);
}
return 0;
}

Monkey and Banana(dp,求最长的下降子序列)的更多相关文章

  1. P1020 导弹拦截(nlogn求最长不下降子序列)

    题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...

  2. JDOJ 1946 求最长不下降子序列个数

    Description 设有一个整数的序列:b1,b2,…,bn,对于下标i1<i2<…<im,若有bi1≤bi2≤…≤bim 则称存在一个长度为m的不下降序列. 现在有n个数,请你 ...

  3. 算法进阶 (LIS变形) 固定长度截取求最长不下降子序列【动态规划】【树状数组】

    先学习下LIS最长上升子序列 ​ 看了大佬的文章OTZ:最长上升子序列 (LIS) 详解+例题模板 (全),其中包含普通O(n)算法*和以LIS长度及末尾元素成立数组的普通O(nlogn)算法,当然还 ...

  4. 【DP】最长不下降子序列问题(二分)

    Description 给你一个长度为n的整数序列,按从左往右的顺序选择尽量多的数字并且满足这些数字不下降. Thinking 朴素dp算法:F[i]表示到第i位为止的最长不下降子序列长度 F[i]= ...

  5. NOIP 2004 T3 合唱队形(DP、最长上升/下降子序列)

    链接:https://ac.nowcoder.com/acm/contest/1082/C来源:牛客网 题目描述 N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队 ...

  6. 求最长不下降子序列(nlogn)

    最长递增子序列问题:在一列数中寻找一些数,这些数满足:任意两个数a[i]和a[j],若i<j,必有a[i]<a[j],这样最长的子序列称为最长递增子序列. 设dp[i]表示以i为结尾的最长 ...

  7. 最长不下降子序列//序列dp

    最长不下降子序列 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 求最长不下降子序列的长度 输入格式 第一行为n,表示n个数第二行n个数 输出格式 最长不下降 ...

  8. tyvj 1049 最长不下降子序列 n^2/nlogn

    P1049 最长不下降子序列 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 求最长不下降子序列的长度 输入格式 第一行为n,表示n个数第二行n个数 输出格式 ...

  9. SPOJ 4053 - Card Sorting 最长不下降子序列

    我们的男主现在手中有n*c张牌,其中有c(<=4)种颜色,每种颜色有n(<=100)张,现在他要排序,首先把相同的颜色的牌放在一起,颜色相同的按照序号从小到大排序.现在他想要让牌的移动次数 ...

随机推荐

  1. iOS开发多线程在实际项目中的运用

    实际项目开发中为了能够给用户更好的体验,有些延时操作我们都会放在子线程中进行. 今天我们就来聊聊多线程在实际项目中的运用. 我们先来看看多线程的基础知识: 1.多线程的原理: 同一时间,CPU只能处理 ...

  2. python3.5 continue和break 项目:买房分期付款(2)

    #案例:买房分期付款24万(10年期限) i=1#定义年份sum1=0while i<=10: print("第",i,"年到了......") if i ...

  3. 操作属性、操作样式 - DOM编程

    1. 操作属性 1.1 HTML 属性与 DOM 属性的对应 <div> <label for="username">User Name: </lab ...

  4. 准确率、精确率、召回率、F1

    在搭建一个AI模型或者是机器学习模型的时候怎么去评估模型,比如我们前期讲的利用朴素贝叶斯算法做的垃圾邮件分类算法,我们如何取评估它.我们需要一套完整的评估方法对我们的模型进行正确的评估,如果模型效果比 ...

  5. pytorch载入模型的参数总是变化,比如说某个conv(3,3)kernel的几个参数总是变化:

  6. 针对VM从挂机-启动后,docker相关服务的无法使用问题!

    使用软件 :VMware  WorkStation 使用系统:linux  centOS 7 windows远程调用软件:xshell 挂机-启动状态后 先使用  service network re ...

  7. 设置与查看Linux系统中的环境变量

    大家好,我是良许. 大家都知道,在 Linux 系统中,有环境变量和 Shell 变量这两种变量. 环境变量是在程序及其子程序中全局可用的,常常用来储存像默认的文本编辑器或者浏览器,以及可执行文件的路 ...

  8. Python 用load_workbook 读取excel某个单元格数据、读取excel行数、列数

    from openpyxl import load_workbook path = r'D:\pywork\12' # EXCEL信息所在文件夹 e= load_workbook(path + '/' ...

  9. mac启动 Apache JMeter 5.3 语言选择中文界面出现乱码 问题解决

    问题重现 问题修复 出现这个问题,是因为,语言与外观不兼容导致,语言选“中文”,外观选“Metal” 细心的你,可能发现,为啥要重启2次呢???第一次设置完语言后,在设置外观,发现菜单不能选择,第二次 ...

  10. Windows下make clean指令错误[错误码2](系统找不到指定文件)的解决方案

    问题来源 因为笔者想用GCC编译器进行Windows下的C语言编程,安装了Mingw-w64的x86_64-posix-seh版本,并按照Visual Studio Code官方的教程,将Mingw- ...