前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HOJ 1438 The Tower of Babylon(线性DP)

HOJ 1438 The Tower of Babylon(线性DP)

作者头像
ShenduCC
发布2018-04-26 11:15:51
5710
发布2018-04-26 11:15:51
举报
文章被收录于专栏:算法修养

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

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

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-01-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档