NYOJ-232 How to eat more Banana

How to eat more Banana

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 4
 
描述
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.
 
输入
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.
输出
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".
样例输入
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
样例输出
Case 1: maximum height = 40
Case 2: maximum height = 21
Case 3: maximum height = 28
Case 4: maximum height = 342
来源
Trinity
 
题意:
  给定n种三个边长不完全相同的长方体,每种长方体的数量无限多,长方体的每个面都可以做底,现要求将这些长方体一个一个摞起来,一个长方体可以摞在另一个长方体上边,当且仅当上边长方体的长和宽必须严格大于下边长方体的长和宽,问在满足要求的情况下最多可以摞起来的高度是多少?
题解:
  将给定的一个长宽高的长方体,将每种可能的方法分别划分为不同的长方体(即按照规定的长宽高放),然后按照长宽从大到小排序,则最后的结果就是LIS的算法了。
 
 
 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 
 6 using namespace std;
 7 
 8 struct node 
 9 {
10     int l;
11     int w;
12     int h;
13 }a[100];
14 int dp[100];
15 
16 int max(int m, int n)
17 {
18     return m > n ? m : n;
19 }
20 
21 int min(int m, int n)
22 {
23     return m < n ? m : n;
24 }
25 
26 bool cmp(node p, node q)
27 {
28     return (p.l > q.l || p.l == q.l && p.w > q.w);
29 }
30 
31 int main()
32 {
33     int n, cnt, x, y, z, max_h;
34     int T = 1;
35     while(scanf("%d", &n), n)
36     {
37         cnt = 0;
38         memset(dp, 0, sizeof(dp));
39         while(n--)
40         {
41             scanf("%d%d%d", &x, &y, &z);
42             
43             a[cnt].l = max(x, y);
44             a[cnt].w = min(x, y);
45             a[cnt++].h = z;
46            
47             a[cnt].l = max(x, z);
48             a[cnt].w = min(x, z);
49             a[cnt++].h = y;
50             
51             a[cnt].l = max(z, y);
52             a[cnt].w = min(z, y);
53             a[cnt++].h = x;
54         }
55         
56         sort(a, a+cnt, cmp);
57 
58         for(int k = 0; k < cnt; ++k)  // 注意别忘了这里 dp 的初始化,让我调试了半天
59             dp[k] = a[k].h;
60 
61         max_h = dp[0];
62         for(int i = 1; i < cnt; ++i)
63         {
64             for(int j = 0; j < i; ++j)
65             {
66                 if(a[j].l > a[i].l && a[j].w > a[i].w && dp[j]+a[i].h>dp[i])
67                     dp[i] = dp[j]+a[i].h;
68             }
69             if(max_h < dp[i])
70                 max_h = dp[i];
71         }
72         printf("Case %d: maximum height = %d\n", T++, max_h);
73     }
74     return 0;
75 }

 

你可能感兴趣的:(NYOJ-232 How to eat more Banana)