HDU 1069 动态规划(DP) Monkey and Banana

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

题意:有n(n<=30)种不同的立方体(每种个数不限),  求能够堆多高.

分析:

(1) 对于每一种立方体, 假设长,宽,高互不相等, 则它置方法有6种不同的情况(长,宽,高全排列).

(2)那么,实际可以看成是6*n种不同的立方体.

(3)对这6*n种立方体长(如果长相等则以宽)小到大排序

(4)这里就等效于有很多的箱子排成了一列, 看怎么才能将它堆得最高,

(5)从小的一边开始, 如果后面的箱子上面能放下前面较小的, 就放一个那种小的那些大箱子上.

(6)然后将这一堆看成一个箱子,只是这个箱子比以前加高了,但底面还是不变.

(7)重复(6)的操作. 直到最后没有任何位置的箱子可以放到其位置的箱子上面时, 找到最高的那个是答案!

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=10000;

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

bool cmp(node A, node B)
{
    if(A.x==B.x) return A.y<B.y;
    return A.x<B.x;
}

int main()
{
    int n,cas=1;
    while(cin>>n&&n)
    {
        int N=0;
        for(int i=0; i<n; ++i)
        {
            int a,b,c;
            cin>>a>>b>>c;
            dp[N].x=a, dp[N].y=b, dp[N].h=c, ++N;
            dp[N].x=b, dp[N].y=a, dp[N].h=c, ++N;
            dp[N].x=c, dp[N].y=b, dp[N].h=a, ++N;
            dp[N].x=b, dp[N].y=c, dp[N].h=a, ++N;
            dp[N].x=a, dp[N].y=c, dp[N].h=b, ++N;
            dp[N].x=c, dp[N].y=a, dp[N].h=b, ++N;
        }
        sort(dp,dp+N,cmp);
        int ans=dp[0].h;
        for(int i=1; i<N; ++i)
        {
            int temp=0;
            for(int j=0; j<i; ++j)
                if( dp[i].x>dp[j].x && dp[i].y>dp[j].y )
                    temp=max(temp,dp[j].h);
            dp[i].h += temp;
            ans = max( ans, dp[i].h );
        }
        printf("Case %d: maximum height = %d\n",cas++,ans);
    }
    return 0;
}



原文地址:https://www.cnblogs.com/javawebsoa/p/3059943.html