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. jquery.fileupload插件 ie9下不支持上传

    根据https://github.com/blueimp/jQuery-File-Upload/wiki/Browser-support The following browsers support ...

  2. nginx配置设置,使部分页面访问跳转到404页面

    location ~* /(ask|hospital|wenda|regsearch|user|doctor) { return ; } error_page /.html;

  3. C# IsBackground作用

    https://blog.csdn.net/snakorse/article/details/43888847 要点: 1.当在主线程中创建了一个线程,那么该线程的IsBackground默认是设置为 ...

  4. logback -- 配置详解 -- 四 -- <filter>

    附: logback.xml实例 logback -- 配置详解 -- 一 -- <configuration>及子节点 logback -- 配置详解 -- 二 -- <appen ...

  5. Java最快的maven仓库地址,国内Maven地址,超快的Maven地址

    Java最快的maven地址,国内Maven地址,超快的Maven地址 >>>>>>>>>>>>>>>> ...

  6. PHP代码审计笔记--代码执行漏洞

    漏洞形成原因:客户端提交的参数,未经任何过滤,传入可以执行代码的函数,造成代码执行漏洞. 常见代码注射函数: 如:eval.preg_replace+/e.assert.call_user_func. ...

  7. mybatis 之 parameterType="java.util.List"

    <!-- 添加 --> <insert id="saveBatchMemberRoleConnRepModel" parameterType="java ...

  8. Python学习(21):Python函数(5):变量作用域与闭包

    转自 http://www.cnblogs.com/BeginMan/p/3179040.html 一.全局变量与局部变量 一个模块中,最高级别的变量有全局作用域. 全局变量一个特征就是:除非被删除, ...

  9. java高级---->Thread之Phaser的使用

    Phaser提供了动态增parties计数,这点比CyclicBarrier类操作parties更加方便.它是jdk1.7新增的类,今天我们就来学习一下它的用法.尘埃落定之后,回忆别来挑拨. Phas ...

  10. ThinkPad L421 如何进入BIOS?(已解决)

    开机屏幕出现ThinkPad标志时,快速按下 F1键 即可进入BIOS