杭电1069--Monkey and Banana 子序列,dp

来源:互联网 发布:串口助手软件下载 编辑:程序博客网 时间:2024/06/04 23:37
                        Monkey and Banana

Problem 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

题意:
题意:有几种边长不等的长方形,每种个数不限,下面就是堆积这些长方形,上一层的面积必须小于下一层的面积,而且,上一层的各个边都不能大于下一层的,指的是各自的边。每个长方形都有高度,求最高能堆积的高度。每个长方形有三种摆放方法。

解题思路:每一个立方体必然有 3 个面积,所以 N 种类型的立方体,必然有 3 * N 个面积组合

将所有的面积从小到大排列之后,每组面积必然对应一个高的值,依据题意,要使高之和最大,所以这个问题就转化成了求最长子列和的最大值 .

代码:

# include <iostream># include <cstdio># include <algorithm>using namespace std;struct Monkey{    int x;    int y;    int hight;    int dp;};/*    题意:        给你若干个长方体,落成一个塔,使上层的面积比下层的小,对应的边也要比下层的小,相等就不行 */bool cmp(Monkey m1,Monkey m2){//用sort函数排序,先按x后按y升序    if(m1.x<m2.x)  return 1;    else if(m1.x==m2.x && m1.y<m2.y)   return 1;    else  return 0;}int main(){    int n,m;    int i,j,k;      Monkey mk[1000];    int x,y,z;//长,宽,高     int icase = 0;    while(scanf("%d",&n),n!=0){        j = 0;        for(i=0;i<n;i++){            scanf("%d%d%d",&x,&y,&z);            if(x==y){                if(y==z)//长宽高相等的时间就一种情况                     mk[j].x = x,mk[j].y = y,mk[j].hight = z,mk[j].dp=mk[j].hight,j++;                   else{//长宽相等,与高不相等  3                     mk[j].x = z,mk[j].y=x,mk[j].hight=y,mk[j].dp=mk[j].hight,j++;                     mk[j].x = x,mk[j].y=z,mk[j].hight=y,mk[j].dp=mk[j].hight,j++;                     mk[j].x = x,mk[j].y=y,mk[j].hight=z,mk[j].dp=mk[j].hight,j++;                   }            }else{//长宽不相等                 if(x==z){                    mk[j].x = z,mk[j].y=x,mk[j].hight=y,mk[j].dp=mk[j].hight,j++;                     mk[j].x = x,mk[j].y=y,mk[j].hight=z,mk[j].dp=mk[j].hight,j++;                     mk[j].x = y,mk[j].y=x,mk[j].hight=z,mk[j].dp=mk[j].hight,j++;                   }else if(y==z){                    mk[j].x = x,mk[j].y=z,mk[j].hight=y,mk[j].dp=mk[j].hight,j++;                     mk[j].x = z,mk[j].y=x,mk[j].hight=y,mk[j].dp=mk[j].hight,j++;                     mk[j].x = y,mk[j].y=z,mk[j].hight=x,mk[j].dp=mk[j].hight,j++;                }else{                    mk[j].x = x,mk[j].y=y,mk[j].hight=z,mk[j].dp=mk[j].hight,j++;                       mk[j].x = y,mk[j].y=x,mk[j].hight=z,mk[j].dp=mk[j].hight,j++;                     mk[j].x = x,mk[j].y=z,mk[j].hight=y,mk[j].dp=mk[j].hight,j++;                    mk[j].x = y,mk[j].y=z,mk[j].hight=x,mk[j].dp=mk[j].hight,j++;                       mk[j].x = z,mk[j].y=x,mk[j].hight=y,mk[j].dp=mk[j].hight,j++;                    mk[j].x = z,mk[j].y=y,mk[j].hight=x,mk[j].dp=mk[j].hight,j++;                }               }        }         sort(mk,mk+j,cmp);    /*        for(i=0;i<j;i++){            printf("%d    %d      %d\n",mk[i].x,mk[i].y,mk[i].hight);        } */        //由于升序排序,需要找大的         int max_s=0;        for(i=1;i<j;i++){            for(k=0;k<i;k++)                if(mk[i].x>mk[k].x && mk[i].y>mk[k].y)                 //  mk[i].dp = max(mk[i].dp,mk[k].hight+mk[i].dp);                 mk[i].dp = max(mk[i].dp,mk[i].hight+mk[k].dp);            if(mk[i].dp>max_s)  max_s = mk[i].dp;        }        printf("Case %d: maximum height = %d\n",++icase,max_s);    }     return 0;}
0 0
原创粉丝点击