DP————1005

题目:The IQ of a monkey

题意:给定数目的木块,木块可以叠加,但是相同底面积的的木块不能放在一起,因为猴子需要一定的面积从一个木快到另一个木块,问在给定的木块中可以确              定的最大的塔高,木块的数量无限。

思路:给定的一组长宽高(x,y,z)中,高度可以是x,y,z中的任意一个,所以一种木块其实是三种木块。

           先按照长相等宽的放在前面,如果不相等,长的在前面

感想:贪心不可以

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
struct node
{
    int l, w, h;
} a[1047];


int dp[1047];
bool cmp(node a, node b)
{
    if(a.l==b.l)
        return a.w>b.w;
    return a.l>b.l;
}
int MAX(int a, int b)
{
return a>b?a:b;
}
int main()
{
    int n,i;
    int c = 0;
    while(scanf("%d",&n)&&n)
    {
        int tt[3];
        int k = 0;
        for(i = 0; i < n; i++)
        {
            scanf("%d%d%d",&tt[0],&tt[1],&tt[2]);
            sort(tt,tt+3);
            a[k].l = tt[0];
            a[k].w = tt[1];
            a[k].h = tt[2];
            k++;
            a[k].l = tt[1];
            a[k].w = tt[2];
            a[k].h = tt[0];
            k++;
            a[k].l = tt[0];
            a[k].w = tt[2];
            a[k].h = tt[1];
            k++;
        }
        sort(a,a+k,cmp);
        int maxx = 0;
        for(i = 0; i < k; i++)
        {
            dp[i] = a[i].h;
            for(int j = i-1; j >= 0; j--)
            {
                if(a[j].l>a[i].l && a[j].w>a[i].w)
                {
                    dp[i] = MAX(dp[i], dp[j]+a[i].h);
                }
            }
            if(dp[i] > maxx)
            {
                maxx = dp[i];
            }
        }
        printf("Case %d: maximum height = %d\n",++c,maxx);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值