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. SQL优化|Java面试题

    转载:https://www.cnblogs.com/Jacck/p/8030455.html 数据库的优化问题 一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂 ...

  2. [GPU] CUDA for Deep Learning, why?

    又是一枚祖国的骚年,阅览做做笔记:http://www.cnblogs.com/neopenx/p/4643705.html 这里只是一些基础知识.帮助理解DL tool的实现. 最新补充:我需要一台 ...

  3. 微信公众号支付-Common

    using System.Web; /// <summary> /// 公共帮助类 /// </summary> public class Common { private H ...

  4. oracle 定义带参数的视图

    1.定义包 CREATE OR REPLACE package p_view_param is --定义开始日期-- function set_beginTime(beginTime varchar2 ...

  5. 手机APP支付--整合支付宝支付控件

    长话短说,本文根据支付宝官方说明文档,简单总结下,并且说明下开发过程碰到的问题以及该如何解决. 整合步骤: 1 登录商家服务网站,下载开发包,地址:https://b.alipay.com/order ...

  6. 8 -- 深入使用Spring -- 2...3 使用@Resource配置依赖

    8.2.3 使用@Resource配置依赖 @Resource 位于javax.annotation包下,是来自Java EE规范的一个Annotation,Spring直接借鉴了该Annotatio ...

  7. 使用dshow捕获摄像头图像

    #include "stdafx.h" #include <DShow.h> #include <Guiddef.h> #include <strmi ...

  8. C++ template —— 模板特化(五)

    本篇讲解模板特化-------------------------------------------------------------------------------------------- ...

  9. STL——关联式容器

    一.关联式容器 标准的STL关联式容器分为set(集合)/map(映射表)两大类,以及这两大类的衍生体multiset(多键集合)和 multimap(多键映射表).这些容器的底层机制均以RB-tre ...

  10. linux查看Raid磁盘阵列信息

    软件raid:只能通过Linux系统本身来查看 cat /proc/mdstat 可以看到raid级别,状态等信息. 硬件raid: 最佳的办法是通过已安装的raid厂商的管理工具来查看,有cmdli ...