题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1069

Monkey and Banana

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16589    Accepted Submission(s): 8834

Problem 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
 
Source
 
 
O(n^3):
1. 设dp[i][j]为第i块放块j的最大高度。
2.状态转移:对于第i块放块j的情况下, 枚举所有块k, 如果块j能放在块k上面, 那么第i块放块j 就可以从 第i-1块放块k 中转移过来了。
即: dp[i][j] = max(dp[i-1][k]+height[j])
 
 
代码如下:
 #include<bits/stdc++.h>
using namespace std;
const int MAXN = ; int block[MAXN][];
int dp[MAXN][MAXN]; int main()
{
int n, kase = ;
while(scanf("%d",&n) && n)
{
int N = ;
for(int i = ; i<=n; i++)
{
int a, b, h;
scanf("%d%d%d", &a, &b, &h); //三种放置如下:
block[++N][] = min(b,h), block[N][] = max(b,h), block[N][] = a;
block[++N][] = min(a,h), block[N][] = max(a,h), block[N][] = b;
block[++N][] = min(a,b), block[N][] = max(a,b), block[N][] = h;
} int ans = -;
memset(dp, , sizeof(dp));
for(int i = ; i<=N; i++) //初始化第一个
dp[][i] = block[i][], ans = max(dp[][i], ans); for(int i = ; i<=N; i++) //第i个
for(int j = ; j<=N; j++) //第i个为块j
for(int k = ; k<=N; k++) //枚举块j下面的块
if(block[j][]<block[k][] && block[j][]<block[k][]) //块j能够放在块k上, 那么就可以转移
dp[i][j] = max(dp[i][j], dp[i-][k]+block[j][]), ans = max(dp[i][j], ans); printf("Case %d: maximum height = %d\n", ++kase, ans);
}
return ;
}

O(n^2):

1.根据长或者宽,对每一种block(每一块block有三种放置方式)进行降序排序。

2.设dp[i]为块i放在最上面的最大高度。

3.对于当前块i, 枚举能够放在它下面的块j(由于经过了排序,所以j的下标为1~i-1),然后把块i放到块j上,更新dp[i]。思想与LIS的O(n^2)写法类似。

4.相同类型的题:HDU1160

代码如下:

 #include<bits/stdc++.h>
using namespace std;
const int MAXN = ; struct node
{
int a, b, h;
bool operator<(const node x){ //对a或者b进行排序(降序)
return a>x.a;
}
}block[MAXN];
int dp[MAXN]; int main()
{
int n, kase = ;
while(scanf("%d",&n) && n)
{
int N = ;
for(int i = ; i<=n; i++)
{
int a, b, h;
scanf("%d%d%d", &a, &b, &h); //三种放置如下:
block[++N].a = min(b,h), block[N].b = max(b,h), block[N].h = a;
block[++N].a = min(a,h), block[N].b = max(a,h), block[N].h = b;
block[++N].a = min(a,b), block[N].b = max(a,b), block[N].h = h;
}
sort(block+, block++N); int ans = -;
for(int i = ; i<=N; i++) //初始化第一个
dp[i] = block[i].h, ans = max(ans, dp[i]); //对于当前i,枚举能够放在它下面的块j,然后跟新dp[i]。
for(int i = ; i<=N; i++)
for(int j = ; j<i; j++)
if(block[i].a<block[j].a && block[i].b<block[j].b)
dp[i] = max(dp[i], dp[j]+block[i].h), ans = max(ans, dp[i]); printf("Case %d: maximum height = %d\n", ++kase, ans);
}
return ;
}

HDU1069 Monkey and Banana —— DP的更多相关文章

  1. kuangbin专题十二 HDU1069 Monkey and Banana (dp)

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

  2. HDU1069:Monkey and Banana(DP+贪心)

    Problem Description A group of researchers are designing an experiment to test the IQ of a monkey. T ...

  3. HDU1069 Monkey and Banana

    HDU1069 Monkey and Banana 题目大意 给定 n 种盒子, 每种盒子无限多个, 需要叠起来, 在上面的盒子的长和宽必须严格小于下面盒子的长和宽, 求最高的高度. 思路 对于每个方 ...

  4. HDU 1069 Monkey and Banana (DP)

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

  5. HDU 1069 Monkey and Banana(DP 长方体堆放问题)

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

  6. HDU 1069 Monkey and Banana dp 题解

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

  7. HDU1069 - Monkey and Banana【dp】

    题目大意 给定箱子种类数量n,及对应长宽高,每个箱子数量无限,求其能叠起来的最大高度是多少(上面箱子的长宽严格小于下面箱子) 思路 首先由于每种箱子有无穷个,而不仅可以横着放,还可以竖着放,歪着放.. ...

  8. HDU1069 Monkey and Banana(dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 题意:给定n种类型的长方体,每个类型长方体无数个,要求长方体叠放在一起,且上面的长方体接触面积要小于 ...

  9. HDU-1069 Monkey and Banana DAG上的动态规划

    题目链接:https://cn.vjudge.net/problem/HDU-1069 题意 给出n种箱子的长宽高 现要搭出最高的箱子塔,使每个箱子的长宽严格小于底下的箱子的长宽,每种箱子数量不限 问 ...

随机推荐

  1. javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean (蛋疼死我了)

    1为抛出异常原因,2为异常解决方法. 原因:   进入spring:bind标签源码你可以看到 Object target = requestContext.getModelObject(beanNa ...

  2. tarjan 学习记

    1.强连通分量是什么 强连通分量指的是部分点的集合如果能够互相到达(例如 1→3,3→2,2→1(有向图)这种,132每个点都能互相抵达) 或者说,有一个环,环上点的集合就是一个强连通分量 2.那怎么 ...

  3. Java学习关于随机数工具类--Random类

    Random类是伪随机数生成器.之所以称为伪随机数(pseudorandom),是因为它们只是简单的均匀分布序列.Random类定义了以下构造函数: Random() Random(long seed ...

  4. 【02】sass更新的方法

    [02]更新的方法   gem install sass         **

  5. PTA 05-树8 File Transfer (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/670 5-8 File Transfer   (25分) We have a netwo ...

  6. B题 Sort the Array

    题目大意:判断能否通过一次倒置,使序列变为一个递增序列 如果可以,输出倒置那一段的起始点和终点的位置: 题目链接:http://codeforces.com/problemset/problem/45 ...

  7. [转]genymotion Unable to load VirtualBox engine 某种解决办法

    genymotion Unable to load VirtualBox engine 某种解决办法 耳闻genymotion这款模拟器很强力.于是下下来试试看.我的机器上是有virtualbox的了 ...

  8. 指针,数组,字符串的区别(高质量程序设计指南C++/C语言第7章)

    指针: 指针是变量,和平时的那些变量没有本质的差异,不同的只是它的值和类型,.,即解释方式 二进制层面:指针的值是内存单元的地址,而变量是引用内存单元值的别名 语言层面:指针的值就是变量的地址. 对象 ...

  9. python学习之-- random模块

    random模块random.random():随机打印一个小数random.randint(1,10):随机打印1-10之间的任意数字(包括1和10)random.randrange(1,10):随 ...

  10. Multiply Strings(字符串乘法模拟,包含了加法模拟)

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...