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种箱子的长宽高 现要搭出最高的箱子塔,使每个箱子的长宽严格小于底下的箱子的长宽,每种箱子数量不限 问 ...
随机推荐
- PhotoshopCS6
download: http://www.playnext.cn/photoshop-cs6.html cracker: http://www.playnext.cn/adobe-cs6-crack. ...
- JavaScript中变量、作用域和内存问题(JavaScript高级程序设计第4章)
一.变量 (1)ECMAScript变量肯能包含两种不同的数据类型的值:基本类型值和引用类型值.基本类型值指的是简单的数据段,引用类型值指那些可能由多个值构成的对象. (2)基本数据类型是按值访问,可 ...
- zoj 2201 No Brainer
No Brainer Time Limit: 2 Seconds Memory Limit: 65536 KB Zombies love to eat brains. Yum. Input ...
- Codeforces Round #277 (Div. 2 Only)
A:SwapSort http://codeforces.com/problemset/problem/489/A 题目大意:将一个序列排序,可以交换任意两个数字,但要求交换的次数不超过n,输出任意一 ...
- 【贪心】codeforces B. Heidi and Library (medium)
http://codeforces.com/contest/802/problem/B [题意] 有一个图书馆,刚开始没有书,最多可容纳k本书:有n天,每天会有人借一本书,当天归还:如果图书馆有这个本 ...
- bzoj 3207 花神的嘲讽计划Ⅰ 主席树+hash
花神的嘲讽计划Ⅰ Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3112 Solved: 1086[Submit][Status][Discuss] ...
- java中filter的用法
filter过滤器主要使用于前台向后台传递数据是的过滤操作.程度很简单就不说明了,直接给几个已经写好的代码: 一.使浏览器不缓存页面的过滤器 Java代码 import javax.servlet ...
- Django的form,model自定制
一.Form组件原理: django框架提供了一个form类,来处理web开发中的表单相关事项.众所周知,form最常做的是对用户输入的内容进行验证,为此django的forms类提供了全面的内容验证 ...
- [bzoj2780][Spoj8093]Sevenk Love Oimaster_广义后缀自动机
Sevenk Love Oimaster bzoj-2780 Spoj-8093 题目大意:给定$n$个大串和$m$次询问,每次给出一个字符串$s$询问在多少个大串中出现过. 注释:$1\le n\l ...
- FM算法及FFM算法
转自:http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html http://blog.csdn. ...