HDU 1069 Monkey and Banana ——(DP)
简单DP。
题意:给出若干种长方体,如果摆放时一个长方体的长和宽小于另一个的长宽,那么它可以放在另一个的上面,问最高能放多少高度。每种长方体的个数都是无限的。
做法:因为每种个数都是无限,那么每种按照x,y,z分别重新排列可以得到6种长方体。现在用dp[i]表示选到第i个且第i个必须使用的最大高度,那么转移是从1~i-1中的dp中选一个能放在i的前面的最大值放在i的前面,再加上第i个的高,就得到了dp[i](如果一个都不能放在i的前面,那就只放i即可)。然后最终答案是dp[1]~dp[n]的最大值。
注意点是在dp前必须按照(x,y)先排序,这样可以保证第i个能够放在它前面的一定在1~i-1中(后面的一定x或y比它大)。
代码如下:
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
using namespace std;
const int N = 1e7 + ;
const int inf = 0x3f3f3f3f; struct node
{
int x,y,z;
bool operator < (const node & temp) const
{
return x == temp.x ? y < temp.y : x < temp.x;
}
}p[]; int dp[]; int main()
{
int kase = ;
int n;
while(scanf("%d",&n) == && n)
{
for(int i=;i<=n;i++)
{
scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
p[i+n] = (node){p[i].x,p[i].z,p[i].y};
p[i+*n] = (node){p[i].y,p[i].x,p[i].z};
p[i+*n] = (node){p[i].y,p[i].z,p[i].x};
p[i+*n] = (node){p[i].z,p[i].x,p[i].y};
p[i+*n] = (node){p[i].z,p[i].y,p[i].x};
}
sort(p+,p++*n);
memset(dp,,sizeof dp);
for(int i=;i<=*n;i++)
{
dp[i] = p[i].z;
for(int j=;j<i;j++)
{
if(p[j].x < p[i].x && p[j].y < p[i].y && dp[j] + p[i].z > dp[i]) dp[i] = dp[j] + p[i].z;
}
}
printf("Case %d: maximum height = %d\n",kase++,*max_element(dp+,dp++*n));
}
}
顺便考虑一个问题,如果要求的是最大能放几个长方体,那就是LIS的问题了。
HDU 1069 Monkey and Banana ——(DP)的更多相关文章
- HDU 1069 Monkey and Banana (dp)
题目链接 Problem Description A group of researchers are designing an experiment to test the IQ of a monk ...
- HDU 1069 Monkey and Banana(动态规划)
Monkey and Banana Problem Description A group of researchers are designing an experiment to test the ...
- HDU 1069 Monkey and Banana(DP——最大递减子序列)
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=1069 题意描述: 给n块砖,给出其长,宽和高 问将这n块砖,怎样叠放使得满足以下条件使得 ...
- HDU 1069 Monkey and Banana (动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 简单记录一下 思路:把长方体的各种摆法都存到数组里面,然后按照长宽排序,再dp即可 转移方程 d ...
- HDU 1069 Monkey and Banana(转换成LIS,做法很值得学习)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS (Java ...
- HDU 1069:Monkey and Banana(DP)
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1069 Monkey and Banana(二维偏序LIS的应用)
---恢复内容开始--- Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1069 Monkey and Banana (动态规划、上升子序列最大和)
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1069 Monkey and Banana 基础DP
题目链接:Monkey and Banana 大意:给出n种箱子的长宽高.每种不限个数.可以堆叠.询问可以达到的最高高度是多少. 要求两个箱子堆叠的时候叠加的面.上面的面的两维长度都严格小于下面的. ...
随机推荐
- SQL优化中的重要概念:事务
原文:SQL优化中的重要概念:事务 sql 优化和事务有关系? 从表面上看,让sql跑的更快,似乎和事务这个概念没什么联系,但是关系数据库中最重要的2个概念就是 关系.事务. 关系,对应到sql中,是 ...
- 利用贝叶斯算法实现手写体识别(Python)
在开始介绍之前,先了解贝叶斯理论知识 https://www.cnblogs.com/zhoulujun/p/8893393.html 简单来说就是:贝叶斯分类是一类分类算法的总称,这类算法均以贝叶斯 ...
- K最近邻算法项目实战
这里我们用酒的分类来进行实战练习 下面来代码 1.把酒的数据集载入到项目中 from sklearn.datasets import load_wine #从sklearn的datasets模块载入数 ...
- optparser模块 与 ZIP爆破(Python)
optparser模块: 为脚本传递命令参数. 初始化: 带 Usage 选项(-h 的显示内容 Usage:): >>> from optparse import OptionPa ...
- js 获取input type="file" 选择的文件大小、文件名称、上次修改时间、类型等信息
文件名的传递 ---全路径获取 $('#file').change(function(){ $('#em').text($('#file').val()); }); 文件名的传递 ---只获取文件名 ...
- redis目录
一.redis基础 二.django-redis 三.redis数据操作详解 四.redis持久化
- centos6/7启动故障排错
centos6启动流程修复: 实验一:删除initramfs-2.6.32-754.el6.x86_64.img进行恢复 该文件很重要initramfs-2.6.32-754.el6.x86_64.i ...
- 将excel表格数据转换为sql语句
今天刚从经理那学到的,迫不及待写下来,以后肯定用得上 1.首先是将excel文件另存为csv格式文件 2.在当前行的最后新增一列,输入下面函数(其中表字段因人而异) =CONCATENATE(&quo ...
- 02.python网络爬虫第二弹(http和https协议)
一.HTTP协议 1.官方概念: HTTP协议是 Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(www.world wide web) 服务器传输超 ...
- RHEL7启动到命令模式
打开/etc/inittab 文件会看到以下信息 从中知道想要启动后就进入完整的多用户文本模式(命令行模式) 以root权限执行: ln -sf /lib/systemd/system/multi-u ...