UVA 437 The Tower of Babylon巴比伦塔

题意:有n(n≤30)种立方体,每种有无穷多个。要求选一些立方体摞成一根尽量高的柱子(可以自行选择哪一条边作为高),使得每个立方体的底面长宽分别严格小于它下方立方体的底面长宽。

评测地址:http://acm.hust.edu.cn/vjudge/problem/19214

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 50010
inline const int read(){
    register int x=0,f=1;
    register char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
struct node{
    int x,y,z;
    bool operator < (const node &t) const{
        return x*y<t.x*t.y;
    } 
}e[N];
int ca,n,cnt,f[N];
void dp(){
    int ans=0;
    for(int i=1;i<=cnt;i++){
        f[i]=e[i].z;
        for(int j=1;j<i;j++){
            if(e[j].x<e[i].x&&e[j].y<e[i].y){
                f[i]=max(f[i],f[j]+e[i].z);
            }
        }
        ans=max(ans,f[i]);
    }
    printf("Case %d: maximum height = %d\n", ++ca, ans);
}
int main(){
    for(;;){
        n=read();
        if(!n) break;
        cnt=0;
        memset(e,0,sizeof e);
        memset(f,0,sizeof f);
        for(int i=1,x,y,z;i<=n;i++){
            x=read();y=read();z=read();
            e[++cnt]=(node){x,y,z};
            e[++cnt]=(node){y,x,z};
            e[++cnt]=(node){x,z,y};
            e[++cnt]=(node){z,x,y};
            e[++cnt]=(node){z,y,x};
            e[++cnt]=(node){y,z,x};
        }
        sort(e+1,e+cnt+1);
        dp();
    }
    return 0;
}

 

posted @ 2016-08-16 21:15  神犇(shenben)  阅读(445)  评论(0编辑  收藏  举报