hdu 1069 dp

来源:互联网 发布:创意中国设计大赛 知乎 编辑:程序博客网 时间:2024/05/21 05:41

题意:给出n个砖块(长,宽,高)  砖块有很多摆法比如10*20*30的砖块  有(10 20 30)(10 30 20)(20 10 30)(20 10 30 )等种摆法

利用这些砖块叠出塔形建筑  即下面的砖块长和宽一定要比上面的大   求满足条件的塔形建筑的最大高度


思路:进行排序按照砖块的长和宽进行升序排序     然后利用DP逐一推倒即可

#include <iostream>
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "math.h"
#include "algorithm"
#include <queue>
using namespace std;
#define modulo 1000000007
int lenth[200],brand[200],height[200],dp[200],pos[200];
int cmp(int a,int b)
{
    if(lenth[a]!=lenth[b]) return lenth[a]<lenth[b];
    return brand[a]<brand[b];
}
int main(void)
{
  int n,x,y,z,temp,maxn,cnt=1;
  //freopen("t","r",stdin);
  while(scanf("%d",&n)!=EOF&&n)
  {
        temp=0;maxn=0;
        for(int i=0;i<n;i++){
            scanf("%d%d%d",&x,&y,&z);
        if(x==y)
        {
            if(y==z) {
                lenth[temp]=x;brand[temp]=x;height[temp]=x;temp++;
            }
            else{
                lenth[temp]=x;brand[temp]=x;height[temp]=z;temp++;
                lenth[temp]=z;brand[temp]=x;height[temp]=x;temp++;
                lenth[temp]=x;brand[temp]=z;height[temp]=x;temp++;
            }
        }
        else {
            if(y==z){
            lenth[temp]=x;brand[temp]=y;height[temp]=y;temp++;
            lenth[temp]=y;brand[temp]=y;height[temp]=x;temp++;
            lenth[temp]=y;brand[temp]=x;height[temp]=y;temp++;
            }
            else {
                if(x==z){
                    lenth[temp]=x;brand[temp]=y;height[temp]=x;temp++;
                    lenth[temp]=x;brand[temp]=x;height[temp]=y;temp++;
                    lenth[temp]=y;brand[temp]=x;height[temp]=x;temp++;
                }
                else{
                    lenth[temp]=x;brand[temp]=y;height[temp]=z;temp++;
                    lenth[temp]=x;brand[temp]=z;height[temp]=y;temp++;
                    lenth[temp]=y;brand[temp]=x;height[temp]=z;temp++;
                    lenth[temp]=z;brand[temp]=x;height[temp]=y;temp++;
                    lenth[temp]=y;brand[temp]=z;height[temp]=x;temp++;
                    lenth[temp]=z;brand[temp]=y;height[temp]=x;temp++;
                }
            }
        }
        }
        for(int i=0;i<temp;i++)
            pos[i]=i;
        sort(pos,pos+temp,cmp);
        for(int i=0;i<temp;i++)
            dp[i]=height[pos[i]];
        for(int i=1;i<temp;i++)
        {
            for(int j=0;j<i;j++)
            {
                if(lenth[pos[j]]<lenth[pos[i]]&&brand[pos[j]]<brand[pos[i]])
                    dp[i]=max(dp[i],dp[j]+height[pos[i]]);
            }
            maxn=max(dp[i],maxn);
        }
        printf("Case %d: maximum height = %d\n",cnt++,maxn);
  }
}


0 0