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. selenium 单浏览器模式

    如果需要无限制的任意实例化,而不是把实例化后的对象传给另一个函数.方法处理.就需要控制单浏览器,不然每次实例化弹出来一个新浏览器,很占内存 #coding=utf8 from selenium.web ...

  2. c# windows服务

    参考:https://www.cnblogs.com/knowledgesea/p/3616127.html 序言 前段时间做一个数据迁移项目,刚开始用B/S架构做的项目,但B/S要寄存在IIs中,而 ...

  3. 8 -- 深入使用Spring -- 4... Spring的AOP

    8.4 Spring的AOP AOP(Aspect Orient Programming),也就是面向切面编程,最为面向对象编程的一种补充. AOP和OOP互为补充,面向对象编程将程序分解成各个层次的 ...

  4. linux 端口占用情况

    1,查看8010端口是否被占用 [root@cloud ~]# netstat -an|grep 8010 tcp 0 0 0.0.0.0:8010 0.0.0.0:* LISTEN 2,查看8010 ...

  5. 使用pyinotify监控文件系统的变化

    pyinotify依赖Linux内核inotify功能,它需要在2.6.13版本的内核的Linux系统上运行. 1. 安装pyinotify pip install pyinotify 安装完后可以直 ...

  6. ISD9160学习笔记02_搭建NuMicro开发环境

    开发环境这边没什么好说的,烧写玩了玩录音的测试程序. 1. 烧写工具 昨晚先尝试了下烧写工具(NuMicro ICP Programming Tool 1.30.6491.exe),板子自带了烧写器, ...

  7. MFC框架程序解析

    MFC的 程序框架: WinMain函数:程序首先到达全局变量theApp,再到达theAPP的构造函数,最后到达WinMain函数处. 问:为何要定义一个全局对象theAPP,让其在WinMain函 ...

  8. Xcode - 添加自定义代码提示

    在开发过程中我们要学会去模仿苹果的一些用法,这样才能让开发更有效率,更规范. 1.苹果自带的代码片段提示 代码片段就是你在Xcode中敲for然后回车,你会看到 for (<#initializ ...

  9. 这样理解 HTTPS 更容易(Maybe)

    摘要:本文尝试一步步还原HTTPS的设计过程,以理解为什么HTTPS最终会是这副模样.但是这并不代表HTTPS的真实设计过程.在阅读本文时,你可以尝试放下已有的对HTTPS的理解,这样更利于“还原”过 ...

  10. 【技术分享会】 @第六期 iOS开发基础

    前言 iOS之前被称为 iPhone OS,是一个由苹果公司开发的移动操作系统. iOS的第一个版本是在2007年发布的,其中包括iPhone和iPod Touch. iOS开发工具:Xcode 运行 ...