HDU1069 Monkey and Banana —— DP
题目链接: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
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.
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.
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
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342
#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的更多相关文章
- kuangbin专题十二 HDU1069 Monkey and Banana (dp)
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU1069:Monkey and Banana(DP+贪心)
Problem Description A group of researchers are designing an experiment to test the IQ of a monkey. T ...
- HDU1069 Monkey and Banana
HDU1069 Monkey and Banana 题目大意 给定 n 种盒子, 每种盒子无限多个, 需要叠起来, 在上面的盒子的长和宽必须严格小于下面盒子的长和宽, 求最高的高度. 思路 对于每个方 ...
- HDU 1069 Monkey and Banana (DP)
Monkey and Banana Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 1069 Monkey and Banana(DP 长方体堆放问题)
Monkey and Banana Problem Description A group of researchers are designing an experiment to test the ...
- HDU 1069 Monkey and Banana dp 题解
HDU 1069 Monkey and Banana 纵有疾风起 题目大意 一堆科学家研究猩猩的智商,给他M种长方体,每种N个.然后,将一个香蕉挂在屋顶,让猩猩通过 叠长方体来够到香蕉. 现在给你M种 ...
- HDU1069 - Monkey and Banana【dp】
题目大意 给定箱子种类数量n,及对应长宽高,每个箱子数量无限,求其能叠起来的最大高度是多少(上面箱子的长宽严格小于下面箱子) 思路 首先由于每种箱子有无穷个,而不仅可以横着放,还可以竖着放,歪着放.. ...
- HDU1069 Monkey and Banana(dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 题意:给定n种类型的长方体,每个类型长方体无数个,要求长方体叠放在一起,且上面的长方体接触面积要小于 ...
- HDU-1069 Monkey and Banana DAG上的动态规划
题目链接:https://cn.vjudge.net/problem/HDU-1069 题意 给出n种箱子的长宽高 现要搭出最高的箱子塔,使每个箱子的长宽严格小于底下的箱子的长宽,每种箱子数量不限 问 ...
随机推荐
- Triangular Pastures (二维01背包)
描述Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectang ...
- POJ 2513 无向欧拉通路+字典树+并查集
题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...
- 【Floyd最短路】第七届福建省赛 FZU Problem 2271 X
http://acm.fzu.edu.cn/problem.php?pid=2271 [题意] 给定一个n个点和m条边的无向连通图,问最多可以删去多少条边,使得每两个点之间的距离(最短路长度)不变. ...
- 【ZJOI2017 Round1练习&BZOJ4767】D1T3 两双手(排列组合,DP)
题意: 100%的数据:|Ax|,|Ay|,|Bx|,|By| <= 500, 0 <= n,Ex,Ey <= 500 思路:听说这是一道原题 只能往右或者下走一步且有禁止点的简化版 ...
- 50个必备的实用jQuery代码段(转载)
本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够帮助 ...
- RedirectAttributes
RedirectAttributes是Spring mvc 3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的 他有两种带参的方式: 第一种: attr.addAttribute(&q ...
- dsu on tree:关于一类无修改询问子树可合并问题
dsu on tree:关于一类无修改询问子树可合并问题 开始学长讲课的时候听懂了但是后来忘掉了....最近又重新学了一遍 所谓\(dsu\ on\ tree\)就是处理本文标题:无修改询问子树可合并 ...
- Ubuntu 16.04安装Fiddler抓包工具(基于Mono,且会有BUG)
说明:Fiddler官方提供了Mono版本的,但是只有2014版本的,不是最新的,并且运行期间会有BUG,比如界面错乱卡死等等,但是勉强能代理,抓SSL的包,如果使用了要做好心理准备.将就一下还是可以 ...
- Windows下C/C++连接mysql数据库的方法
步骤 安装MySQL数据库 项目属性页->C/C++->常规->附加包含目录:xxx\mysql Server 5.6\include 项目属性页->链接器->常规-&g ...
- 【APUE】wait与waitpid函数
当一个进程终止时,内核就向其父进程发送SIGCHLD信号.因为子进程终止是个异步事件,所以这种信号也是内核向父进程发的异步通知.父进程可以选择忽略该信号,或者提供一个该信号发生时即被调用执行的函数.对 ...