Monkey and Banana 简单的动态规划

1.注:

  本人第一篇博客,有啥不足还请多多包涵,有好的建议请指出。你以为有人读你博客,还给你提意见。

2.原题

  Background:

  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(为了好看,前面多了tab,复制时注意一下,第一次写博客,也不知影不影响复制)

  Sample Output

  Case 1: maximum height = 40   

  Case 2: maximum height = 21   

  Case 3: maximum height = 28   

  Case 4: maximum height = 342

3.题目大意

  猴子(为了还原背景。。。)有n种长方体(每种都没有数量限制),长宽高都给你,让你帮猴子计算他能搭多高

  限制:下面的长方体的长和宽都要大于上面的,当然哪条边是长,哪条边是宽,哪条边是高你说了算。

4.分析题目

  猴子很聪明,他们先把问题简单化,我们也试试:

    如果长宽高是给定的,我怎么解决这个问题?

  友好城市这题做过吗?没有的话就先去做一下。类似的,我们这一题也可以像友好城市那样做。

  要求长宽都是递增的,那就先对长或宽进行排序,然后再找另一个的最长上升子序列(当然,这里每个的权值就不一定是1了),找完之后,答案不就出来了吗?

  猴子表示想到这里就可以了,但是我们是人类,要展现人类的智慧:

    我们要把长方体进行旋转。

  咋旋转呢,我们这样想,a,b,c三个,别管怎么放置,每种放置只用一次(因为要求严格上升吗),那就瞬间好办了:

    我把一个长方体变成6个!!!

    那不会用重了吗?哦,兄弟,非常抱歉,请你再读一遍题。

    于是问题迎刃而解。

5.别整没用的,上代码

 #include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=*+;
int Dp[maxn];
struct K{
int x;
int y;
int h;
friend bool operator < (K a,K b){
return a.x<b.x;
}
}tin[maxn];//物品的结构体
int main(){
int n;
int jsjs=;
while(){//这样写大家习惯吗?
jsjs++;
scanf("%d",&n);
if(!n)
break;
memset(Dp,,sizeof(Dp));
for(int i=;i<=n;i++){
scanf("%d%d%d",&tin[i].x,&tin[i].y,&tin[i].h);
tin[i+n].x=tin[i].y;//这里可要想清楚,还有,可整个函数
tin[i+n].y=tin[i].x;
tin[i+n].h=tin[i].h;
tin[i+n*].x=tin[i].y;
tin[i+n*].y=tin[i].h;
tin[i+n*].h=tin[i].x;
tin[i+n*].x=tin[i].h;
tin[i+n*].y=tin[i].y;
tin[i+n*].h=tin[i].x;
tin[i+n*].x=tin[i].x;
tin[i+n*].y=tin[i].h;
tin[i+n*].h=tin[i].y;
tin[i+n*].x=tin[i].h;
tin[i+n*].y=tin[i].x;
tin[i+n*].h=tin[i].y;
}
sort(tin+,tin++*n);
int ans=;
for(int i=;i<=*n;i++){
Dp[i]=tin[i].h;
for(int j=;j<i;j++)//数据范围真的友好
if(tin[i].x>tin[j].x&&tin[i].y>tin[j].y)
Dp[i]=max(Dp[i],Dp[j]+tin[i].h);
ans=max(ans,Dp[i]);
}
printf("Case %d: maximum height = %d\n",jsjs,ans);
}
return ;
}

Monkey and Banana 题解(动态规划)的更多相关文章

  1. HDU 1069 Monkey and Banana(动态规划)

    Monkey and Banana Problem Description A group of researchers are designing an experiment to test the ...

  2. HDU 1069 Monkey and Banana (动态规划、上升子序列最大和)

    Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

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

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

  4. 随手练——ZOJ 1093 Monkey and Banana(动态规划)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=93 一堆科学家研究猩猩的智商,给他M种长方体,每种N个. 然后,将一个 ...

  5. HDU 1069 Monkey and Banana (动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 简单记录一下 思路:把长方体的各种摆法都存到数组里面,然后按照长宽排序,再dp即可 转移方程 d ...

  6. HDU 1069 Monkey and Banana dp 题解

    HDU 1069 Monkey and Banana 纵有疾风起 题目大意 一堆科学家研究猩猩的智商,给他M种长方体,每种N个.然后,将一个香蕉挂在屋顶,让猩猩通过 叠长方体来够到香蕉. 现在给你M种 ...

  7. HDU——Monkey and Banana 动态规划

                                                                       Monkey and Banana Time Limit:2000 ...

  8. Monkey and Banana(HDU 1069 动态规划)

    Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  9. ZOJ 1093 Monkey and Banana (LIS)解题报告

    ZOJ  1093   Monkey and Banana  (LIS)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...

随机推荐

  1. 基于华为云IOT及无线RFID技术的智慧仓储解决方案最佳实践系列一

    [摘要]仓储管理存在四大细分场景:出入库管理.盘点.分拣和货物跟踪.本系列将介绍利用华为云IOT全栈云服务,端侧采用华为收发分离式RFID解决方案,打造端到端到IOT智慧仓储解决方案的最佳实践. 仓储 ...

  2. Elasticsearch 常见错误

    一 read_only_allow_delete" : "true" 当我们在向某个索引添加一条数据的时候,可能(极少情况)会碰到下面的报错: { "error ...

  3. TensorFlow从0到1之TensorFlow Keras及其用法(25)

    Keras 是与 TensorFlow 一起使用的更高级别的作为后端的 API.添加层就像添加一行代码一样简单.在模型架构之后,使用一行代码,你可以编译和拟合模型.之后,它可以用于预测.变量声明.占位 ...

  4. C++ Primer Plus(一)

    完整阅读C++ Primer Plus 系统重新学习C++语言部分,记录重要但易被忽略的,关键但易被遗忘的. 预备 1.C++相对于C增加了最关键的两项,面向对象和范型编程. 处理数据 2.对于变量明 ...

  5. Elasticsearch去重查询/过滤重复数据(聚合)

    带家好,我是马儿,这次来讲一下最近遇到的一个问题 我司某个环境的es中被导入了重复数据,导致查询的时候会出现一些重复数据,所以要我们几个开发想一些解决方案,我们聊了聊,相出了下面一些方案: 1.从源头 ...

  6. Redis的常用配置

    1. 配置守护线程方式运行,修改damonize,使用yes启用守护线程,这样就可以后台运行了 damonize no 修改为 damonize yes 2. 手动指定redis的pid,可以通过pi ...

  7. Java并发编程(05):悲观锁和乐观锁机制

    本文源码:GitHub·点这里 || GitEE·点这里 一.资源和加锁 1.场景描述 多线程并发访问同一个资源问题,假如线程A获取变量之后修改变量值,线程C在此时也获取变量值并且修改,两个线程同时并 ...

  8. 用python玩推理游戏还能掌握基础知识点,有趣又充实,你不试试吗?

    可能更多的人依然还在苦苦的学python各种知识点,但其实同样很多人,玩着游戏就把python学会了.     用python玩推理游戏,是这份python教程中的12个游戏的其中之一. 有关这份Py ...

  9. Merge,Rebase,Cherry-Pick 一文解惑

    代码合并在日常开发中是较为常见的场景,采用合适的合并方式,可以起到事半功倍的效果.对应在 Git 中合并的方式主要有三个,Merge,Rebase,Cherry-Pick. 开始部分会首先介绍一下这三 ...

  10. SpringCloud gateway 3

    参考博客:https://www.cnblogs.com/crazymakercircle/p/11704077.html 1.1 SpringCloud Gateway 简介 SpringCloud ...