Description
 
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
看了几篇博客想了好久才差不多理解...https://blog.csdn.net/lishuhuakai/article/details/8060529 https://blog.csdn.net/qq_34374664/article/details/52249801 这两篇讲的更详细一点
首先是对于输入的长方体的处理,长方体长宽高最多三者两两不同,这里有一个比较重要的思想就是把一个长方体拓展成三个长方体,每个长方体的高是原长方体的长/宽/高。注意,拓展成六个是错误的,因为高一样,剩下两条边x作为长y作为宽和x作为宽y作为长是一样的。在这里可以选择把两边里较长的赋值给x,较短的给y,为了方便判定(当然也可以随机,只不过判定的时候写的会麻烦一点)。为了优化时间,这里可以对结构体数组进行排序(按照面积递减,按照x,y的值递减都可以,按照面积的话比当前面积小的肯定不可能作为底座,按照x,y的话最长边比当前矩形最长边小的肯定也不可能),然后进行线性dp就可以了,dp[i]=max(dp[i],dp[k]+h[i])。注意初始化,假设一个长方体没有任何底座,他自己的高就是最终的高,同时也要注意,排了序以后在当前长方体前面的也不一定能成为底座,还得再判定一下。
#include <bits/stdc++.h>
using namespace std;
struct rectangle
{
int x;
int y;
int z;
int size;
};
bool cmp(rectangle a,rectangle b)
{
// if(a.x!=b.x)return a.x>b.x;
// else return a.y>b.y;
return a.size>b.size;
}
vector<rectangle>v;
int n;
//dp[i]:以第i个为顶的最大高度 dp[i]=max(dp[i],dp[k]+h[i])
int dp[]; int main()
{
int i,j;
int cnt=;
while(scanf("%d",&n)&&n)
{
cnt++;
v.clear();
rectangle occ;
v.push_back(occ);
for(i=;i<=n;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
rectangle temp;
temp.z=x;
temp.x=max(y,z);
temp.y=min(y,z);
temp.size=y*z;
v.push_back(temp);
temp.z=y;
temp.x=max(x,z);
temp.y=min(x,z);
temp.size=x*z;
v.push_back(temp);
temp.z=z;
temp.x=max(x,y);
temp.y=min(x,y);
temp.size=x*y;
v.push_back(temp);
}
sort(v.begin()+,v.end(),cmp);//不要sort占位的
for(i=;i<v.size();i++)//初始化!!假设一个长方体没有任何底座,他自己的高就是最终的高
{
dp[i]=v[i].z;
}
int ans=dp[];
for(i=;i<v.size();i++)
{
for(j=;j<i;j++)//只有i之前的能作为底座
{
if(v[j].x>v[i].x&&v[j].y>v[i].y)
{
dp[i]=max(dp[i],dp[j]+v[i].z); }
ans=max(ans,dp[i]);
}
} printf("Case %d: maximum height = %d\n",cnt,ans);
}
return ;
}

HDU 1069 Monkey and Banana(线性DP)的更多相关文章

  1. HDU 1069—— Monkey and Banana——————【dp】

    Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  2. HDU 1069 Monkey and Banana 基础DP

    题目链接:Monkey and Banana 大意:给出n种箱子的长宽高.每种不限个数.可以堆叠.询问可以达到的最高高度是多少. 要求两个箱子堆叠的时候叠加的面.上面的面的两维长度都严格小于下面的. ...

  3. HDU 1069 Monkey and Banana(DP——最大递减子序列)

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=1069 题意描述: 给n块砖,给出其长,宽和高 问将这n块砖,怎样叠放使得满足以下条件使得 ...

  4. HDU 1069 Monkey and Banana (dp)

    题目链接 Problem Description A group of researchers are designing an experiment to test the IQ of a monk ...

  5. HDU 1069 Monkey and Banana ——(DP)

    简单DP. 题意:给出若干种长方体,如果摆放时一个长方体的长和宽小于另一个的长宽,那么它可以放在另一个的上面,问最高能放多少高度.每种长方体的个数都是无限的. 做法:因为每种个数都是无限,那么每种按照 ...

  6. HDU 1069 Monkey and Banana dp 题解

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

  7. HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径)

    HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径) Description A group of researchers ar ...

  8. HDU 1069 Monkey and Banana(转换成LIS,做法很值得学习)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS (Java ...

  9. HDU 1069 Monkey and Banana (DP)

    Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. 本地连接服务器的redis,jedisCluster创建问题

    1.redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool 查看 ...

  2. Codeforces Round #622 (Div. 2)C(单调栈,DP)

    构造出的结果一定是一个单峰/\这种样子的 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ...

  3. LED Keychain - Widely Used Logo Item

    The LED keychain makes it easy for people to carry their keys with them and carry them with them. It ...

  4. 并查集-F - How Many Tables

    F - How Many Tables 并查集的模板都能直接套,太简单不注释了,就存个代码 #include<bits/stdc++.h> using namespace std; ; i ...

  5. JavaScript对象之get/set方法

    我们可以重写js对象属性的get和set方法. 从上图我们可以看出set和get的语法. 上图则是使用set和get方法对对象的属性进行了输入校验. 从上图可得若对象的原型链上具有不可配置的同名属性( ...

  6. zookeeper入门之介绍与安装

    一:zookeeper是什么 What is ZooKeeper? ZooKeeper is a centralized service for maintaining configuration i ...

  7. kuangbin专题 专题九 连通图 HDU 4738 Caocao's Bridges

    题目链接:https://vjudge.net/problem/HDU-4738 题目:tarjan求桥,坑点: 题目说是分岛任务...如果所有岛之间没有完全连通,就不需要执行任务了...答案直接是0 ...

  8. Python入门1 —— 初识Python

    一:Python介绍 1.Python是什么? Python是一门编程语言,编程语言是一门语言. 语言就是一个事物与另一个事物沟通的工具. 而编程语言则是人与计算机沟通的介质. 2.为什么要跟计算机沟 ...

  9. AcWing 831. KMP字符串

    #include <iostream> using namespace std; , M = ; int n, m; int ne[N];//ne[i] : 以i为结尾的部分匹配的值 ch ...

  10. 题解【洛谷P1347】排序

    题目描述 一个不同的值的升序排序数列指的是一个从左到右元素依次增大的序列,例如,一个有序的数列\(A,B,C,D\) 表示\(A<B,B<C,C<D\).在这道题中,我们将给你一系列 ...