Monkey and Banana(dp,求最长的下降子序列)
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,求最长的下降子序列)的更多相关文章
- P1020 导弹拦截(nlogn求最长不下降子序列)
题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...
- JDOJ 1946 求最长不下降子序列个数
Description 设有一个整数的序列:b1,b2,…,bn,对于下标i1<i2<…<im,若有bi1≤bi2≤…≤bim 则称存在一个长度为m的不下降序列. 现在有n个数,请你 ...
- 算法进阶 (LIS变形) 固定长度截取求最长不下降子序列【动态规划】【树状数组】
先学习下LIS最长上升子序列 看了大佬的文章OTZ:最长上升子序列 (LIS) 详解+例题模板 (全),其中包含普通O(n)算法*和以LIS长度及末尾元素成立数组的普通O(nlogn)算法,当然还 ...
- 【DP】最长不下降子序列问题(二分)
Description 给你一个长度为n的整数序列,按从左往右的顺序选择尽量多的数字并且满足这些数字不下降. Thinking 朴素dp算法:F[i]表示到第i位为止的最长不下降子序列长度 F[i]= ...
- NOIP 2004 T3 合唱队形(DP、最长上升/下降子序列)
链接:https://ac.nowcoder.com/acm/contest/1082/C来源:牛客网 题目描述 N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学排成合唱队 ...
- 求最长不下降子序列(nlogn)
最长递增子序列问题:在一列数中寻找一些数,这些数满足:任意两个数a[i]和a[j],若i<j,必有a[i]<a[j],这样最长的子序列称为最长递增子序列. 设dp[i]表示以i为结尾的最长 ...
- 最长不下降子序列//序列dp
最长不下降子序列 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 求最长不下降子序列的长度 输入格式 第一行为n,表示n个数第二行n个数 输出格式 最长不下降 ...
- tyvj 1049 最长不下降子序列 n^2/nlogn
P1049 最长不下降子序列 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 求最长不下降子序列的长度 输入格式 第一行为n,表示n个数第二行n个数 输出格式 ...
- SPOJ 4053 - Card Sorting 最长不下降子序列
我们的男主现在手中有n*c张牌,其中有c(<=4)种颜色,每种颜色有n(<=100)张,现在他要排序,首先把相同的颜色的牌放在一起,颜色相同的按照序号从小到大排序.现在他想要让牌的移动次数 ...
随机推荐
- 8月份Python招聘情况怎么样?Python爬取招聘数据,并进行分析
前言 拉勾招聘是专业的互联网求职招聘平台.致力于提供真实可靠的互联网招聘求职找工作信息.今天我们一起使用 python 采集拉钩的 python 招聘信息,分析一下找到高薪工作需要掌握哪些技术 开发环 ...
- Java入门到实践系列(1)——Java简介
一.Java的发展历史 Java是由SUN公司的开发人员James Gosling及其领导的一个开发小组与1995年开发并推出的一门高级编程语言.经过二十几年的发展已经成为最受程序员欢迎.使用最为普遍 ...
- 精讲RestTemplate第3篇-GET请求使用方法详解
本文是精讲RestTemplate第3篇,前篇的blog访问地址如下: 精讲RestTemplate第1篇-在Spring或非Spring环境下如何使用 精讲RestTemplate第2篇-多种底层H ...
- Python爬虫开发:反爬虫措施以及爬虫编写注意事项
- Java基础—面向对象特性
1.三大特性 ①.封装 所谓封装,就是将客观事物封装成抽象的类,类的数据和方法只允许可信的类或者对象操作,对不可信的类或对象进行信息隐藏.封装是面向对象的特征之一,是对象和类概念的主要特性.简单的说, ...
- ROS 八叉树地图构建 - 给 octomap_server 增加半径滤波器!
为了在每帧点云中滤除噪声点,选择了半径滤波器,也用高斯滤波器测试过,但是没有半径效果好,这里记录下在 octomap_server 中增加半径滤波器的步骤,并在 launch 中配置滤波器参数. 一. ...
- 用Python爬取股票数据,绘制K线和均线并用机器学习预测股价(来自我出的书)
最近我出了一本书,<基于股票大数据分析的Python入门实战 视频教学版>,京东链接:https://item.jd.com/69241653952.html,在其中用股票范例讲述Pyth ...
- 阿里云日志服务SLS
前言: 刚入职实习了几天,我发现我的任务就是学习阿里云日志服务这块业务内容,这个功能和mysql一样,但是速度和视觉却是甩mysql这类数据库几条街. 当得知公司没人会这项技术后(在这之前我也没听过, ...
- Solon详解(一)- 快速入门
一.Solon 最近号称小而美的的Solon框架,终于得空,搞了一把,发觉Solon确实好用,那Solon到底是什么,又是怎么好用呢? 什么是Solon? Solon参考过Spring boot 和 ...
- Dubbo系列之 (四)服务订阅(1)
辅助链接 Dubbo系列之 (一)SPI扩展 Dubbo系列之 (二)Registry注册中心-注册(1) Dubbo系列之 (三)Registry注册中心-注册(2) Dubbo系列之 (四)服务订 ...