HDU1069

题目大意

给定箱子种类数量n,及对应长宽高,每个箱子数量无限,求其能叠起来的最大高度是多少(上面箱子的长宽严格小于下面箱子)

思路

首先由于每种箱子有无穷个,而不仅可以横着放,还可以竖着放,歪着放。。。长宽高可以有6种形态,用结构体数组存储起来,然后按照长排序,然后就变成了判断是否满足条件,从满足条件中找最大值。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
    int l,w,h;
}a[1005];
int dp[1005];
bool cmp(node x,node y)
{
    if(x.l==y.l)
        return x.w<y.w;
    return x.l<y.l;
}
int main()
{
    int n;
    int c,k,g;
    while(~scanf("%d",&n) && n)
    {
        int temp=0,countt=0;
        for(int i=0;i<n;++i)
        {
            scanf("%d%d%d",&c,&k,&g);
            a[temp].h=c,a[temp].l=k;a[temp++].w=g;
            a[temp].h=c,a[temp].l=g;a[temp++].w=k;
            a[temp].h=k,a[temp].l=c;a[temp++].w=g;
            a[temp].h=k,a[temp].l=g;a[temp++].w=c;
            a[temp].h=g,a[temp].l=k;a[temp++].w=c;
            a[temp].h=g,a[temp].l=c;a[temp++].w=k;
        }
        sort(a,a+temp,cmp);
        dp[0]=a[0].h;
        int maxn=0;
        for(int i=1;i<temp;++i)
        {
            maxn=0;
            for(int j=0;j<i;++j)
                if(a[j].l<a[i].l && a[j].w<a[i].w)
                    maxn=max(maxn,dp[j]);
            dp[i]=a[i].h+maxn;
        }
        maxn=0;
        for(int i=0;i<temp;++i)
            if(dp[i]>maxn)
                maxn=dp[i];
       printf("Case %d: maximum height = %d
",++countt,maxn);
    }
}
原文地址:https://www.cnblogs.com/aerer/p/9930924.html