UVA - 437 The Tower of Babylon

本文介绍了一种将三维几何堆积问题转化为最长递增子序列问题的解决方案,通过将每个立方体拆分为三个部分并按边长排序,最终利用动态规划算法求得最高堆积的高度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

把每个立方体分成是3个,前两个参数(x<=y)表示上表面,第三个表示高h。

然后按照上表面的边长由小到大排序,则原题就变成了最大上升子序列问题(LIS)。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>
#define fir first
#define sed second
#define MP makepair
using namespace std;
typedef unsigned long long LLU;

typedef pair<LLU, LLU> pii;
struct Node
{
    pii p;
    LLU h;
    Node(LLU a, LLU b, LLU h):p(a, b), h(h) {}
    bool operator < (const Node & a)const
    {
        return p.fir<a.p.fir && p.sed<a.p.sed;
    }
};

int cmp(const Node & a, const Node & b)
{
    return a.p<b.p;
}

vector<Node> v;
LLU d[1111];
int main()
{
    int n, kase=0;
    while(cin>>n && n)
    {
        LLU a[3];
        v.clear();
        while(n--)
        {
            for(int i=0; i<3; i++)
                cin>>a[i];
            sort(a, a+3);
            v.push_back(Node(a[0], a[1], a[2]));
            v.push_back(Node(a[0], a[2], a[1]));
            v.push_back(Node(a[1], a[2], a[0]));
        }
        sort(v.begin(), v.end(), cmp);
        for(int i=0; i<v.size(); i++)
            d[i]=v[i].h;
        for(int i=0; i<v.size(); i++)
            for(int j=i+1; j<v.size(); j++)
                if(v[i]<v[j])
                    d[j]=max(d[j], d[i]+v[j].h);
        LLU ans=0;
        for(int i=0; i<v.size(); i++)
            ans=max(ans, d[i]);
        cout<<"Case "<<++kase<<": maximum height = "<<ans<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值