HDU 1069 Monkey and Banana 动态规划

本博客探讨了一个关于砖块叠放的问题,通过排序和动态规划算法找到能够达到的最大高度,详细解释了每一步的逻辑和实现过程。

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

题目大意:

现在又不同规格的砖块若干,要求一个砖块能叠在另外一个砖块上当且仅当接触的面上面的那个砖块的面被下面那个砖块的面包含, 两个相同的底面不能叠放,问叠放的最大高度。


大致思路:

对于每种规格的砖块,至多只能被使用3次(相当于这个砖块的3中摆放状态), 那么多余一种规格的砖,用3中砖记录下来,这样每种砖只能用1次,我们按照每块砖的底面的较长边作为第一关键字,较短边作为第二关键字排序(从小到大)

用dp[ i ] 代表以第 i 块砖作为最底层能达到的高度,那么

dp[ i ] = max( dp[ i ] , dp[ j ] + Hi )  Hi为第 i 块砖的高度

最后的最大高度就是dp[ i ] 中的最大值

代码如下:

Result  :  Accepted     Memory  :  316 KB     Time  :  0 ms

/*
 * Author: Gatevin
 * Created Time:  2014/8/12 20:53:40
 * File Name: hehe.cpp
 */
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

int n,cnt;

struct box
{
    int x,y,z;
    box(int _x, int _y, int _z) : x(_x), y(_y), z(_z)
    {
        if(x > y)
        {
            int tmp = x;
            x = y;
            y = tmp;
        }
    }
    box(){}
};

box b[100];

bool cmp(box u, box v)
{
    return u.x != v.x ? u.x <= v.x : u.y <= v.y;
}

int dp[100];

bool check(int i, int j)
{
    if(b[i].x > b[j].x && b[i].y > b[j].y) return true;
    return false;
}

int main()
{
    int x,y,z;
    int cas = 0;
    while(cin>>n)
    {
        cas++;
        if(!n) return 0;
        cnt = 0;
        while(n--)
        {
            cin>>x>>y>>z;
            b[cnt + 1] = box(x,y,z);
            b[cnt + 2] = box(x,z,y);
            b[cnt + 3] = box(y,z,x);
            cnt += 3;
        }
        sort(b + 1, b + cnt + 1, cmp);
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= cnt; i++)
        {
            dp[i] = b[i].z;
        }
        int answer = 0;
        for(int i = 1; i <= cnt; i++)
        {
            for(int j = 1; j < i; j++)
            {
                if(check(i, j))
                {
                    dp[i] = max(dp[i], dp[j] + b[i].z);
                }
            }
            answer = max(answer, dp[i]);
        }
        printf("Case %d: maximum height = %d\n", cas, answer);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值