Monkey and Banana hdu(1069)

Monkey and Banana

TimeLimit: 2000/1000 MS(Java/Others)    MemoryLimit: 65536/32768 K (Java/Others)
Total Submission(s):3594    AcceptedSubmission(s): 1843


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

The researchers have n types of blocks, and an unlimited supply ofblocks of each type. Each type-i block was a rectangular solid withlinear dimensions (xi, yi, zi). A block could be reoriented so thatany two of its three dimensions determined the dimensions of thebase and the other dimension was theheight. 

They want to make sure that the tallest tower possible by stackingblocks can reach the roof. The problem is that, in building atower, one block could only be placed on top of another block aslong as the two base dimensions of the upper block were bothstrictly smaller than the corresponding base dimensions of thelower block because there has to be some space for the monkey tostep on. This meant, for example, that blocks oriented to haveequal-sized bases couldn't be stacked. 

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

Input
The input file will contain one or more test cases. The first lineof each test case contains an integer n,
representing the number of different blocks in the following dataset. The maximum value for n is 30.
Each of the next n lines contains three integers representing thevalues 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 (theyare numbered sequentially starting from 1) and the height of thetallest possible tower in the format "Case case: maximum height =height".
 

Sample Input
 
   
10 2030 
2
6 810 
5 55 
7
1 11 
2 22 
3 33 
4 44 
5 55 
6 66 
7 77 
31 4159 
26 5358 
97 9323 
84 6264 
33 8327 
0
 
Sample Output
 
   
Case1: maximum height = 40 
Case2: maximum height = 21 
Case3: maximum height = 28 
Case4: maximum height = 342
 

Source

#include <stdio.h>
#include<algorithm>
#include<iostream>
#define MAX 1000
using namespace std;
int max(int a,int b){
    return a>b?a:b;
}
struct ar{
    int a,b,h,s;
}area[3000];
int cmp(ar s,ar t){
    return s.s>t.s;
}
int x[MAX],y[MAX],z[MAX],dp[MAX];
int main(){
    int n,i,j ,w = 1;
    while(~scanf("%d",&n)&&n){
        int h=0;
        for(i=0;i<n;i++)
            scanf("%d%d%d",&x[i],&y[i],&z[i]);
        for(i=0;i<n;i++)
            for(j=0;j<3;j++){
                area[h].a = x[i];
                area[h].b = y[i];
                area[h].h = z[i];
                area[h].s = x[i]*y[i];
                h++;
                swap(x[i],z[i]);
                swap(x[i],y[i]);
            }
        //for(i=0;i<h;i++)
        //  printf("%d %d %d%d\n",area[i].a,area[i].b,area[i].h,area[i].s);
        sort(area,area+h,cmp);
        int ans=0;
        dp[0]=area[0].h;
        for(i=1;i<h;i++){
            dp[i]=area[i].h;
            for(j=0;j<i;j++){
                if(( area[i].a < area[j].a &&area[i].b < area[j].b )||( area[i].a < area[j].b&&area[i].b<area[j].a))
                    dp[i]=max(dp[i],dp[j]+area[i].h);
                
            }
            if(dp[i]>ans)
                ans=dp[i];
        }
        printf("Case %d: maximum height =%d\n",w++,ans);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值