HDU1069:Monkey and Banana(最长上升子序列的应用)

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

这题挺简单的,给定一个箱子的长宽高,要求啰箱子,但必须保证下面箱子的长和宽必须大于上面的箱子。

一个箱子,有六种情况,排序后,按照最长上升子序列来求解就行了。

代码如下:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define inf 0x3f3f3f3f
typedef long long ll;
using namespace std;
int n,tt,dp[210];
struct node
{
    int x,y,z;
} q[210];
int cmp(const void *aa,const void *bb)
{
    struct node *a=(struct node *)aa;
    struct node *b=(struct node *)bb;
    if(a->x!=b->x)
        return b->x-a->x;
    else if(a->y!=b->y)
        return b->y-a->y;
    else return b->z-a->z;
}
int main()
{
    int K=0;
    int xx,yy,zz;
    int maxx;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        ++K;
        tt=1;
        maxx=-inf;
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%d",&xx,&yy,&zz);
            q[tt].x=xx;
            q[tt].y=yy;
            q[tt++].z=zz;

            q[tt].x=xx;
            q[tt].y=zz;
            q[tt++].z=yy;

            q[tt].x=yy;
            q[tt].y=xx;
            q[tt++].z=zz;

            q[tt].x=yy;
            q[tt].y=zz;
            q[tt++].z=xx;

            q[tt].x=zz;
            q[tt].y=xx;
            q[tt++].z=yy;

            q[tt].x=zz;
            q[tt].y=yy;
            q[tt++].z=xx;
        }
        qsort(q+1,tt+1,sizeof(q[1]),cmp);
        memset(dp,0,sizeof(dp));
        dp[1]=q[1].z;
        maxx=max(maxx,q[1].z);
        for(int i=2;i<tt;i++)
        {
            for(int j=1;j<i;j++)
            {
                if(q[i].x<q[j].x&&q[i].y<q[j].y)
                {
                    dp[i]=max(dp[i],dp[j]);
                }
            }
            dp[i]=dp[i]+q[i].z;
            maxx=max(maxx,dp[i]);
        }
        printf("Case %d: maximum height = %d\n",K,maxx);
    }
    return 0;
}

 

posted @ 2015-03-31 16:32  人艰不拆_zmc  阅读(201)  评论(0编辑  收藏  举报