Monkey and Banana HDU

来源:互联网 发布:做淘宝客服的流程 编辑:程序博客网 时间:2024/05/17 10:08
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
110 20 3026 8 105 5 571 1 12 2 23 3 34 4 45 5 56 6 67 7 7531 41 5926 53 5897 93 2384 62 6433 83 270
Sample Output
Case 1: maximum height = 40Case 2: maximum height = 21Case 3: maximum height = 28Case 4: maximum height = 342
       给了你几种不同的规格的盒子,每个盒子可以无限次使用,并且每个盒子由你自己任意摆放,上面的盒子的长宽要严格小于下面盒子的长宽。让你求出这些盒子最高可以重叠多高。
思路:每一种盒子都可以有六个面,其中三个面是不同的,因此由每一种盒子可以拓展出三个不同面作为底面。然后dp【i】的意思就是以第i个盒子作为顶部的最大高度。
如果你把这些盒子作为顶部的值都算出来了,这个问题也就解决了。
dp【i】的状态转移方程:dp【m】m from 1 to i-1+save【i】.h
意思就是找到 在i之前的数值 作为顶部 的最大值的 最大值,加上当前的高度。
ac代码如下:
#include<bits/stdc++.h>using namespace std;struct aa{    int c,k,g;} save[100];int dp[300];bool cmp(const aa &a,const aa &b){    int m=max(a.c,a.k);    int n=max(b.c,b.k);    return m>n;}int main(){    int n;    int kase=1;    while(scanf("%d",&n)!=EOF&&n)    {        memset(dp,0,sizeof(dp));        int m=1;        for(int i=0; i<n; i++)        {            int c,k,g;            scanf("%d%d%d",&c,&k,&g);            save[m].c=c,save[m].k=k,save[m++].g=g;            save[m].c=c,save[m].k=g,save[m++].g=k;            save[m].c=k,save[m].k=g,save[m++].g=c;        }        sort(save+1,save+1+m,cmp);        int maxxx=-1;        for(int i=1; i<m; i++)        {            int maxx=0;            for(int j=i-1; j>=1; j--)            {                if((save[i].c<save[j].c&&save[i].k<save[j].k)||(save[i].c<save[j].k&&save[i].k<save[j].c))                {                    maxx=max(maxx,dp[j]);                }            }            dp[i]=maxx+save[i].g;            maxxx=max(maxxx,dp[i]);        }        printf("Case %d: maximum height = %d\n",kase++,maxxx);    }}




原创粉丝点击