HDU 1069

给你 n 种箱子, 每种箱子无数个,长宽高一直,现在要一个一个垒起来,并且要求上边的必须长宽都小于下边的才可以,

求最大高度。

我们可以发现一个箱子有6个方法叠起来,所以加入一种箱子我们要记录六种情况,

同时,我们发现如果对 长度 L 进行排序,那么我们只需要去判断每个的宽度是否比上一个小就好了,

同时开数组记录, dp[ i ]  代表 使用第 i 个的最高高度。

之后跑一边 n^2 就好了,非常暴力,总复杂度越在 36*n^2 超不掉。

以下是 AC 代码(看了下题解,其实只有三种情况。。只需要对长度一些情况筛选一下会更好。。当然这种能过)

 

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int n;
int dp[maxn],tot;
struct node
{
    int l,w,h;
}nd[maxn];
bool cmp(node a,node b)
{
    if(a.l != b.l)
        return a.l>b.l;
    return a.w<b.w;
}
int main()
{
    int cas=1;
    while(~scanf("%d",&n)&&n)
    {
        int l,w,h;
        tot = 0;
        memset(dp, 0, sizeof dp);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&l,&w,&h);
            nd[++tot].l=l, nd[tot].w=w, nd[tot].h=h;
            nd[++tot].l=l, nd[tot].w=h, nd[tot].h=w;
            nd[++tot].l=w, nd[tot].w=l, nd[tot].h=h;
            nd[++tot].l=w, nd[tot].w=h, nd[tot].h=l;
            nd[++tot].l=h, nd[tot].w=l, nd[tot].h=w;
            nd[++tot].l=h, nd[tot].w=w, nd[tot].h=l;
        }
        sort(nd+1, nd+1+tot, cmp);
        dp[1]=nd[1].h;
        int mx;
        for(int i=2;i<=tot;i++)
        {
            mx = 0;
            for(int j=1;j<i;j++)
            {
                if(nd[j].w>nd[i].w)
                {
                    mx = max(mx, dp[j]);
                }
            }
            dp[i] = nd[i].h + mx;
        }
        int ans = dp[1];
        for(int i=2;i<=tot;i++)
            ans = max(dp[i], ans);
        printf("Case %d: maximum height = %d\n",cas++,ans);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值