HDU1069 Monkey and Banana
HDU1069 Monkey and Banana
题目大意
给定 n 种盒子, 每种盒子无限多个, 需要叠起来, 在上面的盒子的长和宽必须严格小于下面盒子的长和宽, 求最高的高度.
思路
对于每个方块, x, y, z 的全排列共有 6 种可能性, 每种可能性只需要一个方块, 因为方块必须严格小于, 若有两个相同的方块, 则不符合题意.
先将方块按照 x, y 依次进行排序
设 dp[i] 为第 i 个方块时的最高高度, 则每个方块的最高高度为 dp[i] = max(dp[j] + arr[i].z). 每次处理 i 时均默认该方块加入叠成的塔中, 对于后面的状态, 可以不选择这个状态.
代码
package 基础DP1;
import java.util.Arrays;
import java.util.Scanner;
class Block implements Comparable<Block>{
int x, y, z;
Block(int _x, int _y, int _z) {
x = _x; y = _y; z = _z;
}
@Override
public int compareTo(Block arg0) {
if(x != arg0.x)
return x - arg0.x;
return y - arg0.y;
}
}
public class HDU1069 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int round = 0;
while(true) {
int nBlock = in.nextInt();
if(0 == nBlock)
break;
Block[] arr = new Block[nBlock * 6 + 1];
for(int i = 1; i <= nBlock * 6; ) {
int x = in.nextInt();
int y = in.nextInt();
int z = in.nextInt();
arr[i++] = new Block(x, y, z);
arr[i++] = new Block(x, z, y);
arr[i++] = new Block(y, x, z);
arr[i++] = new Block(y, z, x);
arr[i++] = new Block(z, y, x);
arr[i++] = new Block(z, x, y);
}
Arrays.sort(arr, 1, arr.length);
int[] dp = new int[nBlock * 6 + 1];
int ans = 0;
for(int i = 1; i <= nBlock * 6; i++) {
// System.out.println("x is" + arr[i].x + " y is " + arr[i].y + " z is " + arr[i].z);
int maxHeight = 0;
for(int j = 1; j < i; j++) {
if(arr[j].x >= arr[i].x || arr[j].y >= arr[i].y)
continue;
maxHeight = Math.max(maxHeight, dp[j]);
}
dp[i] = maxHeight + arr[i].z;
ans = Math.max(ans, dp[i]);
}
System.out.printf("Case %d: maximum height = %d", ++round, ans);
System.out.println();
}
}
}
HDU1069 Monkey and Banana的更多相关文章
- 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
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS ...
- HDU1069:Monkey and Banana(DP+贪心)
Problem Description A group of researchers are designing an experiment to test the IQ of a monkey. T ...
- HDU-1069 Monkey and Banana DAG上的动态规划
题目链接:https://cn.vjudge.net/problem/HDU-1069 题意 给出n种箱子的长宽高 现要搭出最高的箱子塔,使每个箱子的长宽严格小于底下的箱子的长宽,每种箱子数量不限 问 ...
- HDU1069 - Monkey and Banana【dp】
题目大意 给定箱子种类数量n,及对应长宽高,每个箱子数量无限,求其能叠起来的最大高度是多少(上面箱子的长宽严格小于下面箱子) 思路 首先由于每种箱子有无穷个,而不仅可以横着放,还可以竖着放,歪着放.. ...
- HDU1069 Monkey and Banana(dp)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 题意:给定n种类型的长方体,每个类型长方体无数个,要求长方体叠放在一起,且上面的长方体接触面积要小于 ...
- hdu1069 Monkey and Banana LIS
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #inc ...
- ACM-经典DP之Monkey and Banana——hdu1069
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径)
HDU 1069 Monkey and Banana / ZOJ 1093 Monkey and Banana (最长路径) Description A group of researchers ar ...
随机推荐
- MySQL搭建Amoeba_读写分离
一.背景知识 Amoeba(变形虫)项目,专注 分布式数据库 proxy 开发.座落与Client.DB Server(s)之间.对客户端透明.具有负载均衡.高可用性.sql过滤.读写分离.可路由相关 ...
- php之mongodb插入数据后如何返回当前插入记录ID
<?php /** *插入记录 *参数: *$table_name:表名 *$record:记录 * *返回值: *成功:true *失败:false */ function insert($t ...
- Basic Data Structures and Algorithms in the Linux Kernel--reference
http://luisbg.blogalia.com/historias/74062 Thanks to Vijay D'Silva's brilliant answer in cstheory.st ...
- 转帖:kindeditor编辑区空格被隐藏,导致所见所得不一致的解决办法
1.修改kindereditor-all.js中的 var re = /(\s)<(/)?([\w-:]+)((?:\s+|(?:\s+[\w-:]+)|(?:\s+[\w-:]+=[^\s&q ...
- JavaScript对象 继承
JavaScript继承主要依靠原型链实现. 原型链 利用原型让一个引用类型继承另一个引用类型水位属性和方法. 每一个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指 ...
- 虚拟环境--pipenv
1.安装pipenv,这个工具属于python3 升级pip : pip3 install pipenv 2.在项目中创建虚拟环境 3.激活虚拟环境,进入虚拟环境 进入虚拟环境之前: pipenv s ...
- nodejs 实践:express 最佳实践(四) express-session 解析
nodejs 实践:express 最佳实践(四) express-session 解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs ...
- linux下(ubuntu)反删除(误删恢复)与回收站制作
刚刚有个小伙伴不小心删了他写了好几的天代码,为他心疼之余帮他找回了文件. 想到我之前也常常误删一些文件,就干脆分享一下我的反删除方法,并说说我做的回收站(好low的,求大神指点) 首先是反删除软件ex ...
- 从零开始的全栈工程师——PHP篇 ( 单词汇总 ) ( php解决文字乱码 )
解决乱码: header("Content-Type: text/html;charset=utf-8"); 单词 局部的: local 全局的: global 静态的: stat ...
- caffe-windows中classification.cpp的源码阅读
caffe-windows中classification.cpp的源码阅读 命令格式: usage: classification string(模型描述文件net.prototxt) string( ...