Monkey and Banana (hdu-1069,最长下降(上升)子序和)

题意

给定n个长方体类型,每个长方体类型有无限个

选定若干长方体一个一个堆起来,问可以堆多高?

要求下面的长方体一定比上面的长方体的长宽大

解析:

最长下降(上升)子序和,同时比较x和y

ac:

#include<bits/stdc++.h>
#define MAXN 100005
using namespace std;

struct node
{
    int x,y,h;
    int dp;
}ee[MAXN];

bool cmp(node a,node b)
{
    if(a.x==b.x)
        return a.y>b.y;
    else
        return a.x>b.x;
}


int main()
{
    int n,x,y,h,cas=1;
    while(scanf("%d",&n)&&n)
    {
        int j=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&x,&y,&h);
            if(x==y)
            {
                if(y==h)
                {
                    ee[j].x=x,ee[j].y=y,ee[j].h=h,ee[j].dp=ee[j].h,j++;
                }
                else{
                    ee[j].x=x , ee[j].y=y , ee[j].h=h,ee[j].dp=ee[j].h,j++;
                    ee[j].x=h , ee[j].y=y , ee[j].h=x,ee[j].dp=ee[j].h,j++;
                    ee[j].x=y , ee[j].y=h , ee[j].h=x,ee[j].dp=ee[j].h,j++;
                }
            }
            else{
                if(y==h)
                {
                    ee[j].x=x , ee[j].y=y , ee[j].h=h,ee[j].dp=ee[j].h,j++;
                    ee[j].x=y , ee[j].y=x , ee[j].h=h,ee[j].dp=ee[j].h,j++;
                    ee[j].x=y , ee[j].y=h , ee[j].h=x,ee[j].dp=ee[j].h,j++;
                }
                else if(x==h)
                {
                    ee[j].x=x , ee[j].y=y , ee[j].h=h,ee[j].dp=ee[j].h,j++;
                    ee[j].x=y , ee[j].y=x , ee[j].h=h,ee[j].dp=ee[j].h,j++;
                    ee[j].x=x , ee[j].y=h , ee[j].h=y,ee[j].dp=ee[j].h,j++;
                }
                else
                {
                    ee[j].x=x , ee[j].y=y , ee[j].h=h,ee[j].dp=ee[j].h,j++;
                    ee[j].x=y , ee[j].y=x , ee[j].h=h,ee[j].dp=ee[j].h,j++;

                    ee[j].x=h , ee[j].y=x , ee[j].h=y,ee[j].dp=ee[j].h,j++;
                    ee[j].x=x , ee[j].y=h , ee[j].h=y,ee[j].dp=ee[j].h,j++;

                    ee[j].x=y , ee[j].y=h , ee[j].h=x,ee[j].dp=ee[j].h,j++;
                    ee[j].x=h , ee[j].y=y , ee[j].h=x,ee[j].dp=ee[j].h,j++;
                }
            }
        }
        sort(ee,ee+j,cmp);

        int maxs=0;
        for(int i=1;i<j;i++)
        {
            for(int k=0;k<i;k++)
            {
                if(ee[i].x<ee[k].x&&ee[i].y<ee[k].y)
                    ee[i].dp=max(ee[i].dp,ee[k].dp+ee[i].h);
                maxs=max(maxs,ee[i].dp);
            }
        }

        printf("Case %d: maximum height = %d\n",cas++,maxs);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_41183791/article/details/88410798