HDU-1069

**The
meaning of the question: give you the three parameters length, width and height of n wooden blocks. Each kind of wooden block can be used unlimited times,
but the principle of stacking the wooden blocks is that the length and width of the lower wooden block are greater than the upper one Big
question What is the maximum height that can be stacked?
Idea: This question can be imagined as the longest ascending sub-sequence question has an infinite number of pieces, but the question requires the wooden blocks to be stacked to ensure that the lower length and width are both larger than the upper one. Small, so infinitely many blocks will only be used a limited number of times to output the values ​​of the three sides each time. You only need to
store the side length information of the six possible rotating bodies to implement the sort accordingly. Sorting can be
similar to the longest rise. The realization of the sub-sequence has to be reversed. Don’t stack
it from the bottom up, but start from the top
**

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>

using namespace std;
typedef long long ll;


struct node
{
    
    
    int l,w,h;//长宽高
}bl[227];
int dp[227];
bool cmp(node a,node b)
{
    
    
    if(a.l == a.l)
        return a.w < b.w;
    else
        return a.l < b.l;
}

int main()
{
    
    
    int n;
    int cas = 0;
    while(scanf("%d",&n)&&n)
    {
    
    
        int cnt = 0;
        for(int i = 1;i <= n;i ++)
        {
    
    
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            bl[++cnt].l = a;bl[cnt].w = b;bl[cnt].h = c;
            bl[++cnt].l = a;bl[cnt].w = c;bl[cnt].h = b;
            bl[++cnt].l = b;bl[cnt].w = a;bl[cnt].h = c;
            bl[++cnt].l = b;bl[cnt].w = c;bl[cnt].h = a;
            bl[++cnt].l = c;bl[cnt].w = a;bl[cnt].h = b;
            bl[++cnt].l = c;bl[cnt].w = b;bl[cnt].h = a;
        }
        sort(bl+1,bl+1+cnt,cmp);
        for(int i = 1;i <= cnt;i ++)
        {
    
    
            dp[i] = bl[i].h;//初始化 bl就相当于a[i];
        }
        for(int i = 1;i <= cnt;i ++)
        {
    
    
            for(int j = 1;j < i;j ++)
            {
    
    
                if(bl[j].l < bl[i].l && bl[j].w < bl[i].w)
                {
    
    
                    dp[i] = max(dp[j]+bl[i].h,dp[i]);
                }
            }
        }
        sort(dp+1,dp+1+cnt);
        printf("Case %d: maximum height = %d\n",++cas,dp[cnt]);
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45672411/article/details/104757839