HDU 1069 Monkey and Banana (最长上升子序列)

来源:互联网 发布:如何做数据统计 编辑:程序博客网 时间:2024/05/16 14:36

Monkey and Banana

Time Limit: 2000/1000 MS (Java/Others)   

Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4979    Accepted Submission(s): 2550

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".

 

View Code
  1 /*  2   (1)  3   虽然此题说明每种砖头可以有无限多个,但是其实最多只能用三次。因为题目说明了  4   每块砖头的长和宽都必须比它下面那块砖头的小。所以每种砖头最多只有三种情况使用,  5   假设一条边为a,则它可以作为底面的长或者宽,或者侧面的高。这样可以把一种砖头  6   分成三种砖头,且这三种砖头各一个来使用。  7   (2)  8   将这些分好的砖头先按底面的长升序排序,若长相等则按宽升序排序,这样目的是前面  9   的砖头的底面长和宽都是小于或者等于后面的。假设原来有n种砖头,经过拆分就应该 10   有3*n种数量都是一个的砖头,再经过排序过后,就构成了最长上升子序列的结构了。 11   找到尽可能长的上升子序列,(此时求的不是序列的长度,而是高度,即每块砖头的 12   侧面的高的和) 13    14   15MS    232K  15   */ 16 #include <iostream> 17 #include <cstdio> 18 #include <cstring> 19 #include <algorithm> 20 #define SIZE 205 21  22 using namespace std; 23  24 struct node 25 { 26     int x,y,z; 27 }; 28  29 node bk[SIZE]; 30 int dp[SIZE]; 31 int idx; 32 int n; 33  34 bool cmp(node a,node b) 35 { 36     if(a.x<b.x) 37         return true; 38     else if(a.x==b.x) 39     { 40         if(a.y<b.y) 41             return true; 42         return false; 43     } 44     return false; 45 } 46  47 int main() 48 { 49     int Case = 1; 50     while(~scanf("%d",&n) && n) 51     { 52         int x,y,z; 53         idx = 0; 54         for(int i=1; i<=n; i++) 55         { 56             scanf("%d%d%d",&x,&y,&z); 57  58             bk[++idx].x = x; 59             bk[idx].y = y; 60             bk[idx].z = z; 61  62             bk[++idx].x = y; 63             bk[idx].y = x; 64             bk[idx].z = z; 65  66             bk[++idx].x = x; 67             bk[idx].y = z; 68             bk[idx].z = y; 69  70             bk[++idx].x = z; 71             bk[idx].y = x; 72             bk[idx].z = y; 73  74             bk[++idx].x = y; 75             bk[idx].y = z; 76             bk[idx].z = x; 77  78             bk[++idx].x = z; 79             bk[idx].y = y; 80             bk[idx].z = x; 81         } 82         sort(bk+1,bk+idx+1,cmp); 83         memset(dp,0,sizeof(dp)); 84         int ans = 0; 85         for(int i=1; i<=idx; i++) 86         { 87             int maxi = 0; 88             for(int j=1; j<=idx; j++) 89             { 90                 if(bk[i].x > bk[j].x && bk[i].y > bk[j].y && maxi < dp[j]) 91                     maxi = dp[j]; 92                 if(bk[i].x == bk[j].x && bk[i].y == bk[j].y) 93                     dp[j] = maxi + bk[j].z; 94                 if(dp[j] > ans) 95                     ans = dp[j]; 96             } 97         } 98         printf("Case %d: maximum height = %d\n",Case++,ans); 99     }100     return 0;101 }

 

原创粉丝点击