UVa437,The Tower of Babylon

转:http://blog.csdn.net/wangtaoking1/article/details/7308275

题意为输入若干种立方体(每种若干个),然后将立方体堆成一个塔,要求接触的两个面下底面的长宽分别严格大于上底面,求塔的最大高度。

将每种立方体的各种摆放形式均视为不同的立方体,并存起来。再将所有立方体按照下底面的面积从小到大排序(因为在塔上面的立方体的底面积一定比下面的小),然后只需求该序列的最大上升子序列的长度即可。

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;

struct node          //记录每个立方体的长宽高
{
    int x,y,z;
    void f(int a, int b,int c)
    {
        x=a; y=b; z=c;
    }
}st[200];
bool comp(node a, node b)  //按立方体的底面积从小到大进行排序
{
    if( a.x*a.y <b.x*b.y )
        return 1;
    return 0;
}
int n,m, x,y,z,dp[200];
int main()
{
    int flag =1;
    while( scanf("%d", &n) &&n )
    {
        m=0;
        int i,j;
        for( i=0; i<n; i++)
        {
            scanf("%d%d%d", &x, &y, &z);
            st[ m++].f(x,y,z);     //将6种立方体均保存起来
            st[ m++].f(x,z,y);
            st[ m++].f(y,z,x);
            st[ m++].f(y,x,z);
            st[ m++].f(z,x,y);
            st[ m++].f(z,y,x);
        }
        sort( st, st+m, comp);
        int t=0;
        for( i=0; i<m; i++)          //求最长上升子序列
        {
            dp[i] =st[i].z;
            for( j=0; j<i; j++)
                if( st[i].x >st[j].x && st[i].y >st[j].y )
                    dp[i] =max( dp[i], dp[j] +st[i].z);
            if( dp[i] >t)
                t =dp[i];
        }
        printf("Case %d: maximum height = %d\n",flag++,t);
    }
    return 0;
}

该方法要比紫薯上提供的方法要好的多。清晰易懂。

题意虽说有若干个立方体,但仔细想想,答案说生成的序列中最多可能包含3个同一个立方体(再仔细想想,应该是两个,但是我们还需要看成3个),故将一个立方体拓展成三个立方体即可。

将所有立方体按照下底面的面积从小到大排序(其实也可以对长度一级排序,对宽度二级排序),然后用if( st[i].x >st[j].x && st[i].y >st[j].y )  判断能否状态转移

时间: 2024-08-24 11:59:29

UVa437,The Tower of Babylon的相关文章

uva437 poj2241 The Tower of Babylon dp

// 这题开始在算法竞赛入门经典的书上状态表示 // dp[i][j]表示前i个方块以第j条边为高所能得到的最大高度值 // dp[i][j] = max(dp[0...i-1][0,1,2]+block[i][j]); // 就是一个DAG模型 // 这样记忆化搜索就行啦,还是有些技巧的 // // 第二种做法就是递推 // 首先把一个方块变为6个,即表示长,宽,高 // 当然,首先得要把底面积从大到小排序 // dp[i]表示前i种方块所能达到的最大高度 // dp[i] = max(dp[

uva437 - The Tower of Babylon(DAG上的DP)

题目:uva437 - The Tower of Babylon(DAG上的DP) 题目大意:给你一些立方体,给出长宽高XYZ.现在希望你将这些立方题叠起来,使得最后的高度最大,并且这些立方体是可以无限次使用的,但是一个立方体要在另一个立方体的上面的话是需要满足这个立方体的底面是可以完全包含在下面的那个立方体的底面. 解题思路:其实这里的无限次使用没有什么用,因为一个立方体最多使用三次就不可能是再用.输入的一个立方体其实可以变成三个确定长宽高的立体.然后将这些立方体先做预处理,如果立方体j能够放

UVa 437 The Tower of Babylon(动态规划)

传送门 Description 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

UVa 437 The Tower of Babylon

Description 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 typ

UVA The Tower of Babylon

The Tower of Babylon 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

UVA 437 The Tower of Babylon DP

有向图DAG The Tower of Babylon Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many details of this tale have been forgotten. So n

POJ 2241 The Tower of Babylon

The Tower of Babylon Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 224164-bit integer IO format: %lld      Java class name: Main Perhaps you have heard of the legend of the Tower of Babylon. Nowadays many d

POJ2241——The Tower of Babylon

The Tower of Babylon Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2207   Accepted: 1244 Description 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

poj2241 The Tower of Babylon

The Tower of Babylon 题意:给你n种石头,长x,宽y,高z,每种石头数目无限,一块石头能放到另一块上的条件是:长和宽严格小于下面的石头.问叠起来的最大高度. /* 有些类似“叠箱子”问题,看起来一种砖有无限多个,其实最多只能用到两次. 说下我的思路吧,一块砖有3个数据,虽然3!=6,但本质上还是只有3种,把这三种都表示出来,使x<=y:这样就有了3n组数据.因为我不会建图,就把这3n组数据再排列一下,使一块砖只能放到它后面的砖之上,而绝不能放到之前的砖上,即按x为一级y为二级