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种箱子的长宽高 现要搭出最高的箱子塔,使每个箱子的长宽严格小于底下的箱子的长宽,每种箱子数量不限 问 ...
随机推荐
- Linux常用命令大全 --- 文件备份和压缩命令
在linux中,常用的文件压缩工具有gzip.bzip2.zip . bzip2是最理想的压缩工具,它提供了最大限度的压缩.zip 兼容性好windows也支持 1.bzip2 命令 在shell 提 ...
- jQuery对象数据缓存Cache原理及jQuery.data详解
网上有很多教你怎么使用jQuery.data(..)来实现数据缓存,但有两个用户经常使用的data([key],[value])和jQuery.data(element,[key],[value])几 ...
- python 数据库操作产生中文乱码的解决办法
1.执行python mysql数据库查询操作时,产生中文乱码 #!/usr/bin/python # -*- coding: UTF-8 -*- import MySQLdb db = MySQLd ...
- iis性能监控
文章:对于IIS上的应用程序池监控 文章:IIS并发连接数及性能优化
- HDU1166-敌兵布阵,线段数模板题~~
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- 日志不得应用情况切换强制standby改变状态为primary
日志不得应用情况切换备库为主库 备库运行如下: alter database recover managed standby database disconnect from session; alt ...
- F5 TCP Traffic Flow v0.5
300dpi高清版下载地址 http://down.51cto.com/data/2332253
- 安装Android SDK(东软开源镜像介绍)
启动 Android SDK Manager ,打开主界面,依次选择「Tools」.「Options...」,弹出『Android SDK Manager - Settings』窗口: 在『Andro ...
- 洛谷P2814 家谱(gen)
题目背景 现代的人对于本家族血统越来越感兴趣. 题目描述 给出充足的父子关系,请你编写程序找到某个人的最早的祖先. 输入输出格式 输入格式: 输入由多行组成,首先是一系列有关父子关系的描述,其中每一组 ...
- Codevs 二叉树遍历问题 合集
2010 求后序遍历 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 白银 Silver 题目描述 Description 输入一棵二叉树的先序和中序遍历序列,输出其后序遍历序列. ...