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. Kaggle-pandas(3)

    Summary-functions-and-maps 教程 在上一教程中,我们学习了如何从DataFrame或Series中选择相关数据. 正如我们在练习中所展示的,从我们的数据表示中提取正确的数据对 ...

  2. 验证Kubernetes YAML的最佳实践和策略

    本文来自Rancher Labs Kubernetes工作负载最常见的定义是YAML格式的文件.使用YAML所面临的挑战之一是,它相当难以表达manifest文件之间的约束或关系. 如果你想检查所有部 ...

  3. 为何选择spark!

    随着大数据处理的应用场景越来越多,人们对Hadoop的要求也越来越高,开发出的对应的系统也越来越多,人们迫切的需要一个综合的计算框架,Spark应运而生,我们可以看看Spark可以干些什么. 那么为什 ...

  4. 代码生成器插件与Creator预制体文件解析

    前言 之前写过一篇自动生成脚本的工具,但是我给它起名叫半自动代码生成器.之所以称之为半自动,因为我觉得全自动代码生成器应该做到两点:代码生成+自动绑定.之前的工具只做了代码生成,并没有做自动绑定,所以 ...

  5. EfficientNet

  6. kafka-clients 1.0 高阶API消费消息(未完)

    消费消息的请求(按序) org/apache/kafka/common/requests/RequestHeader org/apache/kafka/common/requests/ApiVersi ...

  7. 理解正向代理&反向代理

    通常的代理服务器,只用于代理内部网络对Internet的连接请求,客户机必须指定代理服务器,并将本来要直接发送到Web服务器上的http请求发送到代理服务器中.由于外部网络上的主机并不会配置并使用这个 ...

  8. jQuery - AJAX笔记

    @ 目录 什么是AJAX 关于 jQuery 与 AJAX jQuery AJAX 参考手册 jQuery ajax - ajax() 方法 定义和用法 语法 参数 options async bef ...

  9. vue-cli的安装及版本查看/更新

    vue-cli的安装及版本查看更新 vue-cli安装 npm install vue-cli -g vue-cli的版本查看 vue -V vue-cli的3.0+以后使用的不是vue-cli了,如 ...

  10. Java之reflection(反射机制)——通过反射操作泛型,注解

    一.反射操作泛型(Generic) Java采用泛型擦除机制来引入泛型.Java中的泛型仅仅是给编译器Javac使用的,确保数据的安全性和免去强制类型转换的麻烦.但是编译一旦完成,所有和泛型有关的类型 ...