The Tower of Babylon

题意:

有n个,长x宽y高z的长方体,把这些长方体摞起来,上面长方体底面的长宽一定要小于下面的,求能摞的最大高度。

分析:

一个长方体,可以有三种放法,先把所有放的状态存起来,按底面升序排列,dp[i]前i个能构成的最大高度,dp[i]=max(dp[i],dp[j]+h)  h为当前长方体高度

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int maxn = 1e5+100;
const int mod =  1000000007;
struct Block{
    int x;
    int y;
    int z;
}b[210];
bool cmp(Block u,Block v){
    if(u.x==v.x)return u.y>v.y;
    else return u.x>v.x;
}
int dp[210],n;
int solve(int num){
    memset(dp,0,sizeof(dp));
    sort(b,b+num,cmp);
    int ma=-1;
    for(int i=0;i<num;++i){
            dp[i]=b[i].z;
        for(int j=0;j<i;++j){
            if(b[j].y>b[i].y&&b[j].x>b[i].x)
            dp[i]=max(dp[i],dp[j]+b[i].z);
        }
        if(dp[i]>ma)
            ma=dp[i];
    }
    return ma;
}
int main()
{int t=0,xx,yy,zz;
    while(~scanf("%d",&n)){
            if(n==0)break;
            t++;
            int num=0;
        for(int i=0;i<n;++i){
            scanf("%d%d%d",&xx,&yy,&zz);
            b[num].x=xx;b[num].y=yy;b[num].z=zz;
            num++;
            b[num].x=yy;b[num].y=xx;b[num].z=zz;
            num++;
            b[num].x=zz;b[num].y=yy;b[num].z=xx;
            num++;
            b[num].x=yy;b[num].y=zz;b[num].z=xx;
            num++;
            b[num].x=xx;b[num].y=zz;b[num].z=yy;
            num++;
            b[num].x=zz;b[num].y=xx;b[num].z=yy;
            num++;
        }
       /* for(int i=0;i<num;++i)
            printf("%d %d %d\n",b[i].x,b[i].y,b[i].z);*/
         printf("Case %d: maximum height = %d\n",t,solve(num));
    }
return 0;
}
时间: 2024-10-20 01:23:51

The Tower of Babylon的相关文章

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

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

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

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

UVA 437 十九 The Tower of Babylon

The Tower of Babylon Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 437 Appoint description:  System Crawler  (2015-08-29) Description Perhaps you have heard of the legend of the Tower of Babylon. No

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

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

UVa 437 The Tower of Babylon(DP 最长条件子序列)

 题意  给你n种长方体  每种都有无穷个  当一个长方体的长和宽都小于另一个时  这个长方体可以放在另一个上面  要求输出这样累积起来的最大高度 因为每个长方体都有3种放法  比较不好控制   可以把一个长宽高分成三个长方体  高度是固定的  这样就比较好控制了 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define maxn 105 int x[max

poj2241 The Tower of Babylon

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