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种箱子的长宽高 现要搭出最高的箱子塔,使每个箱子的长宽严格小于底下的箱子的长宽,每种箱子数量不限 问 ...
随机推荐
- 2016年工作中遇到的问题41-50:Dubbo注册中心奇葩问题,wifi热点坑了
41.获得JSON中的变量.//显示json串中的某个变量,name是变量名function json(json,name){ var jsonObj = eval(json); return jso ...
- cf839c Journey
大水题 #include <iostream> #include <cstdio> using namespace std; int n, du[100005], hea[10 ...
- 使现有的VSCode成为便携版(绿色版)
VSCode可以说是各种代码编辑器前端之中的神器了,相对体积小且扩展性强,我们希望将它携带在U盘中在各种工作环境中使用,官方也提供了在Windows,Linux和MacOS三大平台中使VSCode便携 ...
- [NOIP2003] 提高组 洛谷P1039 侦探推理
题目描述 明明同学最近迷上了侦探漫画<柯南>并沉醉于推理游戏之中,于是他召集了一群同学玩推理游戏.游戏的内容是这样的,明明的同学们先商量好由其中的一个人充当罪犯(在明明不知情的情况下),明 ...
- 【HDOJ6300】Triangle Partition(极角排序)
题意:给定3n个点,保证没有三点共线,要求找到一组点的分组方案使得它们组成的三角形之间互不相交. n<=1e3 思路:以y为第一关键字,x为第二关键字,按x递减,y递增排序 #include&l ...
- Gearman 初窥【转载】
Gearman是一个分发任务的程序框架,可以用在各种场合,与Hadoop相 比,Gearman更偏向于任务分发功能.它的任务分布非常简单,简单得可以只需要用脚本即可完成.Gearman最初用于Live ...
- 352. Data Stream as Disjoint Interval
Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...
- poj2186 求有向图G中所有点都能到达的点的数量
/*题意:有向图,求这样的点的数量:所有点都能到达它.缩点成有向无环图,思:如果该强连通有出度,那么 从该出度出去的边必然回不来(已经缩点了),所以有出度的强连通必然不是.那么是不是所有出度为0的强连 ...
- Swift--错误集:Class controller has not initializers
bug错误图 解决方法: 如下图所示,visitor这个属性并没有拆包处理,及将UIViewController的子类中的变量全部进行拆包处理,就是在变量声明的时候加一个?号,在使用的时候拆包处理,加 ...
- Codeforces 653D Delivery Bears【二分+网络流】
题目链接: http://codeforces.com/problemset/problem/653/D 题意: x个熊拿着相同重量的物品,从1号结点沿着路走到N号结点,结点之间有边相连,保证可以从1 ...