HDU 1069 Monkey and Banana (类似最长递增子序列)

来源:互联网 发布:阿里云备案订单号查询 编辑:程序博客网 时间:2024/06/05 14:38

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069



题意:有n个长方体,给出长宽高,且每一种长方体有3种摆放方法,当一个长方体的长和宽分别大于另一个长方体,便可将这个长方体置于另一个长发体下面,高度则是这2个长方体的高之和,现在每一种长发体都有无数个,问最多可以叠多高


思路:这道题思路类似于求最长递增子序列,先将每个长方体的3种摆放方法转换出来(要注意l比w大,方便后面比较),一同按照长和宽排序,dp[i]意思是以长方体i为底的最大高度,因为经过了排序,所以从小的长方体开始处理。




#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define maxn 130using namespace std;struct Node{    int l,w,h;}s[maxn];int dp[maxn];int num;bool cmp(Node p,Node q){    if (p.l!=q.l) return p.l>q.l;    else return p.w>q.w;}void change(int x,int y,int z){    s[num].l=x;    s[num].w=y;    s[num++].h=z;    s[num].l=y;    s[num].w=z;    s[num++].h=x;    s[num].l=x;    s[num].w=z;    s[num++].h=y;}int main(){    int n,cas=0;;    while (scanf("%d",&n)!=EOF)    {        if (n==0) break;        cas++;        num=0;        for (int i=0;i<n;i++)        {            int tmp[5];            scanf("%d%d%d",&tmp[0],&tmp[1],&tmp[2]);            sort(tmp,tmp+3);            change(tmp[2],tmp[1],tmp[0]);        }        sort(s,s+num,cmp);        for (int i=0;i<num;i++)        {            dp[i]=s[i].h;        }        int res=dp[num-1];        for (int i=num-2;i>=0;i--)        {            for (int j=i+1;j<num;j++)            {                if (s[i].l>s[j].l && s[i].w>s[j].w)                {                    dp[i]=max(dp[j]+s[i].h,dp[i]);                }                res=max(dp[i],res);            }        }        printf("Case %d: maximum height = %d\n",cas,res);    }}




0 0