【HDU - 1069】Monkey and Banana (最长下降子序列 + 贪心,最长上升子序列类问题)

题干:

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

题目大意:

把给定的长方体(不限)叠加在一起,叠加的条件是,上面一个长方体的长和宽都比下面长方体的长

 

和宽短;求这些长方体能叠加的最高的高度.(其中(3,2,1)可以摆放成(3,1,2)、(2,1,3)等).

PS:

每块积木最多有 3 个不同的底面和高度,我们可以把每块积木看成三个不同的积木,

那么n种类型的积木就转化为3 * n个不同的积木,对这3 * n个积木的长按照从大到小排序;

然后找到一个递减的子序列,使得子序列的高度和最大。

解题报告:

     不懂为什么要按照长度排序。

AC代码:

#include<bits/stdc++.h>using namespace std;struct Node {int sm,bg,h;//小,大Node(){}Node(int sm,int bg,int h):sm(sm),bg(bg),h(h){}} node[300000 + 5];
int dp[300000 + 5];
int top;
int n;//bool cmp(Node a,Node b) {
//	if(a.bg > b.bg) return a.sm > b.sm;
//	else if(a.bg == b.bg) return a.sm > b.sm;
//	return 0;//即  b一定排在a前面了 
//}
bool cmp(Node a,Node b) {if(a.bg == b.bg) return a.sm > b.sm;else return a.bg > b.bg;
}
int main()
{int l,w,h;//长宽高 int iCase = 0;while(scanf("%d",&n)) {if(n == 0 ) break;top = 0;memset(dp,0,sizeof(dp));for(int i = 1; i<=n; i++) {scanf("%d%d%d",&l,&w,&h);if(l>w) node[++top] =Node(w,l,h);else   node[++top] =Node(l,w,h);if(l<h) node[++top] = Node(l,h,w);else node[++top] = Node(h,l,w);if(w<h) node[++top] = Node(w,h,l);else node[++top] = Node(h,w,l);}sort(node+1,node+top+1,cmp);
//		for(int i = 1; i<=top; i++) {
//			printf("%d  %d  %d\n",node[i].sm,node[i].bg,node[i].h);
//		}
//		printf("\n");for(int i = 1; i<=top; i++) dp[i] = node[i].h;for(int i = 1; i<=top; i++) {for(int j = 1; j<=i; j++) {if(node[i].bg < node[j].bg && node[i].sm < node[j].sm) {dp[i] = max(dp[i],dp[j] + node[i].h);}}}printf("Case %d: maximum height = %d\n",++iCase,*max_element(dp+1,dp+top+1));}return 0 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441003.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

c 语言定义2维字符串数组赋值,二维数组赋值字符串 c 语言 二维字符串数组赋值问题...

C语言中二维字符数组应该怎样赋值&#xff1f;c语言二维数组如何定义字符串&#xff1f;&#xff1f;&#xff1f;&#xff1f;急。。。二维字符数组的定义格式为&#xff1a;char 数组名[第一维大小][第二维大小]; 例如&#xff1a;char c[3][10]; //定义了一个3行10列的二维字…

【牛客 - 297D】little w and Exchange(上下界贪心)

题干&#xff1a; 旅行到K国的小w发现K国有着很多物美价廉的商品&#xff0c;他想要买一些商品。 结果一掏钱包&#xff0c;包里只剩下n张K国的纸币了&#xff0c;说起来也奇怪&#xff0c;K国纸币并不像其他国家一样都是1元&#xff0c;5元&#xff0c;10元…而是各种奇怪的…

c语言程序设计 doc,《C语言程序设计》.doc

《C语言程序设计》.doc《C语言程序设计》实验 编实验一 C程序的运行环境和运行C程序的方法2实验二 数据类型、运算符和表达式9实验三 最简单的C程序设计14实验四 逻辑结构程序设计20实验五 循环结构程序设计26实验六 数组31实验七 函数39实验八 编译预处理命令45实验九 指针50…

【牛客 - 315F】美丽的项链(线性dp,递推,我为人人)

题干&#xff1a; 妞妞参加了Nowcoder Girl女生编程挑战赛, 但是很遗憾, 她没能得到她最喜欢的黑天鹅水晶项链。 于是妞妞决定自己来制作一条美丽的项链。一条美丽的项链需要满足以下条件: 1、需要使用n种特定的水晶宝珠 2、第i种水晶宝珠的数量不能少于li颗, 也不能多于…

撞球编程c语言,急!C语言编程题——撞球

满意答案#include #include #include int main(){double length,wide,x0,y0,x1,y1;int i;char towards[1500];while(1){memset(towards,0,sizeof(towards));if(scanf("%lf %lf",&wide,&length)EOF)break;scanf("%lf %lf",&x0,&y0);scanf(&…

【牛客 - 315C】排列(思维,贪心,同优则立证明法)

题干&#xff1a; 妞妞得到一个(1~n)的排列p1, p2, p3,...,pn, 听村里的老人牛牛说如果让这个排列变为: 对于所有的1 < i < n, 都满足pi ≠ i, 就可以获得Google Girl Hackathon的入场券。 妞妞仅允许的操作是: 交换排列中两个相邻的元素, 并且妞妞允许做这个操作任意…

镇江 linux技术支持,东云镇江服务器

弹性云服务器 ECS弹性云服务器(Elastic Cloud Server)是一种可随时自助获取、可弹性伸缩的云服务器&#xff0c;帮助用户打造可靠、安全、灵活、高效的应用环境&#xff0c;确保服务持久稳定运行&#xff0c;提升运维效率三年低至5折&#xff0c;多种配置可选了解详情什么是弹性…

*【牛客 - 315D】打车(贪心,同优则立证明法)

题干&#xff1a; 妞妞参加完Google Girl Hackathon之后,打车回到了牛家庄。 妞妞需要支付给出租车司机车费s元。妞妞身上一共有n个硬币&#xff0c;第i个硬币价值为p[i]元。 妞妞想选择尽量多的硬币&#xff0c;使其总价值足以支付s元车费(即大于等于s)。 但是如果从妞妞…

c语言中只能逐个引用6,C语言前面六个练习.doc

C语言前面六个练习第一章 C语言基础知识4&#xff0e;一个函数的函数体可以没有变量定义和执行部分&#xff0c;函数可以是空函数2&#xff0e;一个函数由两部分组成&#xff0c;它们是 函数体 和 函数的说明部分。3&#xff0e;函数体的范围是 大括号以内 。(0级)4&#xff0e…

【牛客 - 289K】这是一个沙雕题III(贪心,思维枚举,技巧trick,计算上下界)

题干&#xff1a; 因为现在的新生太强了&#xff0c;都学会了“dp”&#xff0c;所以就有了这样一个“dp”题&#xff0c;双11时Gugugu有(x&#xff0c;x1,x2....y-1,y)元的抵用券无数张&#xff0c;但是Gugugu有强迫症所以他希望他使用抵扣券正好能够抵扣k元&#xff0c;这…

c 跟r语言运行速度,1. R语言运行效率分析(5)

方法5&#xff1a; 采用 which 语句1: 自定义函数# digital was translated into englishnameMonth_name_whichMonth_nameMonth_name[(which(Month_name1))]Month_name[(which(Month_name2))]Month_name[(which(Month_name3))]Month_name[(which(Month_name4))]Month_name[(whi…

【牛客 - 297C】little w and Segment Coverage(差分数组,区间差分,思维,卡线段树)☆

题干&#xff1a; 小w有m条线段&#xff0c;编号为1到m。 用这些线段覆盖数轴上的n个点&#xff0c;编号为1到n。 第i条线段覆盖数轴上的区间是L[i]&#xff0c;R[i]。 覆盖的区间可能会有重叠&#xff0c;而且不保证m条线段一定能覆盖所有n个点。 现在小w不小心丢失了一…

链表c语言stl,C++STL之List容器

1.再谈链表List链表的概念再度出现了&#xff0c;作为线性表的一员&#xff0c;C的STL提供了快速进行构建的方法&#xff0c;为此&#xff0c;在前文的基础上通过STL进行直接使用&#xff0c;这对于程序设计中快速构建原型是相当有必要的&#xff0c;这里的STL链表是单链表的形…

*【牛客 - 318B】签到题(单调栈,水题)

题干&#xff1a; 众所周知&#xff0c;IG是英雄联盟S8世界总决赛冠军&#xff0c;夺冠之夜&#xff0c;数亿人为之欢呼&#xff01; 赛后某百分百胜率退役ADC选手的某表情包意外走红&#xff0c;某苟会长看到此表情包也想模仿。 于是有n个友爱的萌新决定每人都送会长一根长…

c 语言车牌识别系统课题设计,车牌识别系统的设计--课程设计报告.doc

车牌识别系统的设计--课程设计报告目录一、摘要:3二、设计目的和意义:32.1、设计目的&#xff1a;32.2、设计意义&#xff1a;3三、设计原理:3四、详细设计步骤:34.1、提出总体设计方案:44.2、各模块的实现:5五、设计结果及分析20六、总结:22七、体会23八、参考文献:23一、摘要…

*【HDU - 2586】How far away ? (LCA模板题,倍增)

题干&#xff1a; There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int th…

android volley 上传图片 和参数,Android使用Volley上传文件

一个项目中用到的使用Volley上传头像文件的例子/*** Created by wangshihui on 2015/11/30.* 上传文件* url&#xff1a;.....method&#xff1a;post参数&#xff1a;file接口给的参数&#xff1a;file 就是表单的key&#xff0c;传给mFilePartName;这是个测试类&#xff0c;…

【HDU - 4056】Draw a Mess (并查集 or 线段树)

题干&#xff1a; Its graduated season, every students should leave something on the wall, so....they draw a lot of geometry shape with different color. When teacher come to see what happened, without getting angry, he was surprised by the talented achiev…

android 按钮按下缩放,android捏缩放

我TextView使用本教程为我实现了一个缩放缩放。结果代码是这样的&#xff1a;private GestureDetector gestureDetector;private View.OnTouchListener gestureListener;并在onCreate()中&#xff1a;// Zoom handlersgestureDetector new GestureDetector(new MyGestureDetec…

【CodeForces - 520B】Two Buttons (bfs或dp或时光倒流,trick)

题干&#xff1a; Vasya has found a strange device. On the front panel of a device there are: a red button, a blue button and a display showing some positive integer. After clicking the red button, device multiplies the displayed number by two. After click…