Monkey and Banana Hdoj

Description
A group of researchers are designing an experiment to test the IQ of a monkey. They will hang a banana at the roof of a building, and at the mean time, provide the monkey with some blocks. If the monkey is clever enough, it shall be able to reach the banana by placing one block on the top another to build a tower and climb up to get its favorite food. 

The researchers have n types of blocks, and an unlimited supply of blocks of each type. Each type-i block was a rectangular solid with linear dimensions (xi, yi, zi). A block could be reoriented so that any two of its three dimensions determined the dimensions of the base and the other dimension was the height. 

They want to make sure that the tallest tower possible by stacking blocks can reach the roof. The problem is that, in building a tower, one block could only be placed on top of another block as long as the two base dimensions of the upper block were both strictly smaller than the corresponding base dimensions of the lower block because there has to be some space for the monkey to step on. This meant, for example, that blocks oriented to have equal-sized bases couldn't be stacked. 

Your job is to write a program that determines the height of the tallest tower the monkey can build with a given set of blocks. 

Input
The input file will contain one or more test cases. The first line of each test case contains an integer n, 
representing the number of different blocks in the following data set. The maximum value for n is 30. 
Each of the next n lines contains three integers representing the values xi, yi and zi. 
Input is terminated by a value of zero (0) for n. 

Output
For each test case, print one line containing the case number (they are numbered sequentially starting from 1) and the height of the tallest possible tower in the format "Case case: maximum height = height". 

Sample Input
1
10 20 30
2
6 8 10
5 5 5
7
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
5
31 41 59
26 53 58
97 93 23
84 62 64
33 83 27
0 

Sample Output
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342
///题意,给你n个长方体,并且每个长方体都可以使用多次,要你求出用这些长方体可以堆出塔的最大高度。题意很明确,下意识想到用贪心,在纸上画图却发现贪心似乎并不对,其实只要保证每个长方体来重叠的速度变小,就可以得到最大的塔的高度。
///于是开个结构题开始敲,对于每个长方体,将长,宽,高的位置全部用输入的3个数试一遍,得到所有的排列即可,开始想用next_permulation搞一下(呜呜~~)结果发现这却并不正确,所以最自己手动生成了排列,dp部分就很简单了,把最长上升子序列的算法修改一下就行了,1A还是非常高兴的,毕竟dp我很少1A,代码姿势比较丑,仅供参考。
///0ms Accepted 23356kb

///@author just_sort
///2015/9/10 17:16
///offer.

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

struct node{
   int x,y,z;
};
struct node ans[162];
int dp[162];
bool cmp(const node &a,const node &b)
{
    if(a.x==b.x)
        return a.y<b.y;
    return a.x<b.x;
}
int compare(const node &a,const node &b);
bool compare2(int a,int b,int c,int d,int e,int f);

int main()
{
    int n;
    int kcase=1;
    while(scanf("%d",&n)==1)
    {
        if(n==0)
            break;
        memset(dp,0,sizeof dp);
        int cnt(0);
        int a[3];
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&a[0],&a[1],&a[2]);
            if(a[0]==a[1]&&a[1]==a[2])
              ans[cnt++]=(node){a[0],a[0],a[0]};
            else
            {
                for(int i=0;i<3;i++)
                {
                    for(int j=0;j<3;j++)
                    {
                        for(int k=0;k<3;k++)
                        {
                            if(compare2(a[i],a[j],a[k],a[0],a[1],a[2]))
                            {
                                ans[cnt++]=(node){a[i],a[j],a[k]};
                            }
                        }
                    }
                }
            }
        }
            sort(ans,ans+cnt,cmp);
            int maxsum=-1;
            /*puts("");
            for(int i=0;i<cnt;i++)
            {
                printf("%d %d %d  \n",ans[i].x,ans[i].y,ans[i].z);
            }
            */
            ///引用最长上升子序列表算法,其中把a[i]替换成node型的
            for(int i=0;i<cnt;i++)
            {
                dp[i]=ans[i].z;
            }
            for(int i=0;i<cnt;i++)
            {
                for(int j=0;j<i;j++)
                {
                    if(compare(ans[j],ans[i])==1)
                    {
                        dp[i]=max(dp[i],dp[j]+ans[i].z);
                    }
                }
                maxsum=max(maxsum,dp[i]);
            }
        printf("Case %d: maximum height = %d\n",kcase++,maxsum);
       ///     printf("%d\n",maxsum);
        }
        return 0;
}

int compare(const node &a,const node &b)
{
    if(a.x<b.x&&a.y<b.y)
        return 1;
    return 0;
}

bool compare2(int a,int b,int c,int d,int e,int f)
{
    int tmp1[3],tmp2[3];
    tmp1[0]=a,tmp1[1]=b,tmp1[2]=c,tmp2[0]=d,tmp2[1]=e,tmp2[2]=f;
    sort(tmp1,tmp1+3);
    sort(tmp2,tmp2+3);
    for(int i=0;i<3;i++)
        if(tmp1[i]!=tmp2[i])
          return false;
    return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值