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. Unity Editor Console Pro 扩展点击定位到外部工程

    链接 http://blog.csdn.net/akof1314/article/details/53232981 http://forum.china.unity3d.com/thread-2689 ...

  2. 为什么一段时间后网站后台自动退出 php中session过期时间设置

    修改php配置文件中的session.gc_maxlifetime.如果想了解更多session回收机制,继续阅读.(本文环境php5.2) 概述:每一次php请求,会有1/100的概率(默认值)触发 ...

  3. 使用awstat分析Nginx的访问日志

    在我的上一篇文章<使用 Nginx 提升网站访问速度>中介绍了 Nginx 这个 HTTP 服务器以及如何通过它来加速网站的访问速度.在实际的网站运营中,我们经常需要了解到网站的访问情况, ...

  4. solidity合约面向对象

    1. 属性[状态变量]的访问权限 public  internal[合约属性默认的权限]  private 说明:属性默认访问全向为internal,internal和private类型的属性,外部是 ...

  5. 个人MySQL股票数据库的建立日记

    #!/usr/bin/python# -*- coding: UTF-8 -*- import tushare as tsfrom sqlalchemy import create_engine co ...

  6. rest-framework框架——视图三部曲

    一.mixins类编写视图 1.配置url urlpatterns = [ ... re_path(r'^authors/$', views.AuthorView.as_view(), name=&q ...

  7. Swiper双向轮播

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  8. C语言字符数组与字符串

    研究几个案例: 输出图案: #include <stdio.h> void main() { ][] = { {', ' ', ' '}, {', ' '}, {'}, {', ' '}, ...

  9. 标头停止点不能位于宏或#if块中.

    使用VS2010在项目中编写C++头文件**.h时提示: PCH 警告: 标头停止点不能位于宏或#if块中 原因:vs2010的智能感知要求.h必须以非#if系列的预编译指令打头 正确方法:将所有含有 ...

  10. java面试题之----mysql表优化方案

    本文转载自segmentfault,原文链接:https://segmentfault.com/a/1190000006158186. 当MySQL单表记录数过大时,增删改查性能都会急剧下降,可以参考 ...