The Tower of Babylon

My Tags

Cancel - Seperate tags with commas.

Source : University of Ulm Internal Contest 1996

Time limit : 5 sec Memory limit : 32 M

Submitted : 303, Accepted : 155

Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So now, in line with the educational nature of this contest, we will tell you the whole story:

The babylonians had 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 wanted to construct the tallest tower possible by stacking blocks. The problem was 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. 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 babylonians can build with a given set of blocks.

Input Specification

The input file will contain one or more test cases. The first line of each test case contains an integer n,

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.

Output Specification

For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format “Case case: maximum height = height

Sample Input

1

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

Sample Output

Case 1: maximum height = 40

Case 2: maximum height = 21

Case 3: maximum height = 28

Case 4: maximum height = 342

思路:

一个长方体可以有三种摆放方式,将所有摆放方式按照长,宽排序,随便哪个优先,然后再求最大上升子序列,上升的含义是严格的长减少,宽减少

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h> using namespace std;
struct Node
{
int x;
int y;
int z;
}a[100];
int cmp(Node a,Node b)
{
if(a.x==b.x)
return a.y>b.y;
return a.x>b.x;
}
int n;
int dp[100];
int vis[1000][1000]; int main()
{
int l,w,h;
int cas=0;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
memset(vis,0,sizeof(vis));
int cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&l,&w,&h);
Node term;
term.x=l>w?l:w;term.y=l>w?w:l;term.z=h;
a[++cnt]=term;
Node term2;
term2.x=l>h?l:h;term2.y=l>h?h:l;term2.z=w;
a[++cnt]=term2;
Node term3;
term3.x=w>h?w:h;term3.y=w>h?h:w;term3.z=l;
a[++cnt]=term3; }
sort(a+1,a+cnt+1,cmp);
a[cnt+1].x=-1;a[cnt+1].y=-1;a[cnt+1].z=0;
for(int i=1;i<=cnt+1;i++)
{
int num=0;
for(int j=i-1;j>=1;j--)
{
if(a[i].x<a[j].x&&a[i].y<a[j].y)
{
num=max(num,dp[j]);
}
}
dp[i]=num+a[i].z;
}
printf("Case %d: maximum height = %d\n",++cas,dp[cnt+1]);
}
return 0;
}

HOJ 1438 The Tower of Babylon(线性DP)的更多相关文章

  1. uva The Tower of Babylon[LIS][dp]

    转自:https://mp.weixin.qq.com/s/oZVj8lxJH6ZqL4sGCXuxMw The Tower of Babylon(巴比伦塔) Perhaps you have hea ...

  2. UVa 437 The Tower of Babylon(DP 最长条件子序列)

     题意  给你n种长方体  每种都有无穷个  当一个长方体的长和宽都小于还有一个时  这个长方体能够放在还有一个上面  要求输出这样累积起来的最大高度 由于每一个长方体都有3种放法  比較不好控制 ...

  3. UVa 437 The Tower of Babylon(经典动态规划)

    传送门 Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details ...

  4. UVA 437 十九 The Tower of Babylon

    The Tower of Babylon Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Subm ...

  5. POJ2241——The Tower of Babylon

    The Tower of Babylon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2207   Accepted: 1 ...

  6. UVA437-The Tower of Babylon(动态规划基础)

    Problem UVA437-The Tower of Babylon Accept: 3648  Submit: 12532Time Limit: 3000 mSec Problem Descrip ...

  7. DAG 动态规划 巴比伦塔 B - The Tower of Babylon

    题目:The Tower of Babylon 这是一个DAG 模型,有两种常规解法 1.记忆化搜索, 写函数,去查找上一个符合的值,不断递归 2.递推法 方法一:记忆化搜索 #include < ...

  8. [动态规划]UVA437 - The Tower of Babylon

     The Tower of Babylon  Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many d ...

  9. POJ 2241 The Tower of Babylon

    The Tower of Babylon Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Or ...

随机推荐

  1. messagpack的使用

    我打算使用messagepack对通信的对象进行序列化,使用的方式参考这篇文章: http://www.cppfans.org/1410.html 此处记录一下自己遇到的一些问题 先是用VS2010对 ...

  2. springboot学习过程笔记

    1.spring-boot-devtools热部署在IDEA中配置后不起作用(Eclipse设置了自动编译,所以不用额外设置) 1).pom.xml添加spring-boot-devtools依赖后 ...

  3. 【AI】图像识别-物体检测-百度AI-EasyDL-NodeJS

    var https = require('https') var express = require('express'); var app = express(); var bodyParser = ...

  4. 【虚拟机】安装vmtools之后任然不能在虚拟机和主机之间复制粘贴的问题

    一.卸载 ../bin/vmware-uninstall-tools.pl rm -rvf /usr/lib/vmware-tools apt-get install open-vm-tools-de ...

  5. Data Guard启动实时日志应用

    1. REDO数据实时应用 启动实时应用的优势在于,REDO数据不需要等待归档完成,接收到即可被应用,这样执行角色切换时,操作能够执行得更快,因为日志是被即时应用的. 要启动实时应用也简单,前提是St ...

  6. Python学习--字符串slicing

    Found this great table at http://wiki.python.org/moin/MovingToPythonFromOtherLanguages Python indexe ...

  7. CMake INSTALL 命令设置exe dll lib的安装位置

    install(TARGETS ${OUT_NAME} RUNTIME DESTINATION ${CMAKE_BINARY_DIR}/bin LIBRARY DESTINATION ${CMAKE_ ...

  8. Android studio导入eclipse工程时出现中文全部乱码问题

    File->settings->Copyright的下面File Encoding -> 改成gbk这样修改就OK了

  9. Jar命令

    JAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包;当然也是有区别的,JAR包中有一个META-INF\MANIFEST.MF文件,当你打成JAR包时,它会自动生成. 一.ja ...

  10. 第三篇:配置Hadoop的Eclipse开发环境

    前言 在先前的文章中,已经介绍了如何在Ubuntu Kylin操作系统下搭建Hadoop运行环境,而现在将在之前工作的基础上搭建Eclipse开发环境. 配置 开发环境:Eclipse 4.2,其他同 ...