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的更多相关文章

  1. kuangbin专题十二 HDU1069 Monkey and Banana (dp)

    Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  2. HDU1069 Monkey and Banana —— DP

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS ...

  3. HDU1069:Monkey and Banana(DP+贪心)

    Problem Description A group of researchers are designing an experiment to test the IQ of a monkey. T ...

  4. HDU-1069 Monkey and Banana DAG上的动态规划

    题目链接:https://cn.vjudge.net/problem/HDU-1069 题意 给出n种箱子的长宽高 现要搭出最高的箱子塔,使每个箱子的长宽严格小于底下的箱子的长宽,每种箱子数量不限 问 ...

  5. HDU1069 - Monkey and Banana【dp】

    题目大意 给定箱子种类数量n,及对应长宽高,每个箱子数量无限,求其能叠起来的最大高度是多少(上面箱子的长宽严格小于下面箱子) 思路 首先由于每种箱子有无穷个,而不仅可以横着放,还可以竖着放,歪着放.. ...

  6. HDU1069 Monkey and Banana(dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 题意:给定n种类型的长方体,每个类型长方体无数个,要求长方体叠放在一起,且上面的长方体接触面积要小于 ...

  7. hdu1069 Monkey and Banana LIS

    #include<cstdio> #include<iostream> #include<algorithm> #include<queue> #inc ...

  8. ACM-经典DP之Monkey and Banana——hdu1069

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  9. 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 ...

随机推荐

  1. org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.

    今天报了这个异常,这是页面报的 org.springframework.dao.DataIntegrityViolationException: could not execute statement ...

  2. 32位x86处理器编程导入——《x86汇编语言:从实模式到保护模式》读书笔记08

    在说正题之前,我们先看2个概念. 1.指令集架构(ISA) ISA 的全称是 instruction set architecture,中文就是指令集架构,是指对程序员实际"可见" ...

  3. java多线程之线程组与线程池

    看这篇文章:http://blog.csdn.net/zen99t/article/details/50909099

  4. 【Linux相识相知】计算机的组成、linux发行版和哲学思想、基础命令和目录结构(FHS)

    从今天开始,Frank将开始在博客上记录自己学习linux的点点滴滴,F初来乍到,还望各位大佬多多指教.本次博客的主要内容如下: 计算机基础:简要的描述了计算机的组成及其功能: linux初识:介绍了 ...

  5. Spring.NET入门

    Spring.NET入门  http://www.cnblogs.com/haogj/archive/2011/06/10/2077540.html http://www.cnblogs.com/ha ...

  6. windows下配置nginx环境

    一. nginx软件拷贝 拷贝 nginx文件 到对应的服务目录中 如 E:/service/nginx nginx文件地址: 链接: http://pan.baidu.com/s/1c25oq6O ...

  7. sql查询结果多对多转为一对多返回前端

    企业表 ent_EnterpriseArchives  有id,企业名称 entName veh_Vehicle 车辆表,有所属企业id  companyId,车辆id,车牌号licPlate 目的是 ...

  8. mybatis一对多映射

    场景: A:SecControlRulePojo.java B:SecControlSubRulePojo C:SecControlSubRuleManyPojo 实体A中包含List<B> ...

  9. Bash 终端快捷键

    在终端下输大量的命令,有些快捷键还是挺有帮助的. 跳跃 跳到最后 ctrl + e (end) 跳到最前 ctrl + a (abcde....) 向后跳一个单词 alt + 右方向键 向前跳一个单词 ...

  10. html-标题标签、水平线标签和特殊字符

    标题标签  <h1></h1> <h2></h2> ...... <h6></h6> 从h1到h6,大小是依次变小,同时会自动换 ...