Monkey and Banana (HDU - 1069)
题面
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”.
样例输入
11
210 20 30
32
46 8 10
55 5 5
67
71 1 1
82 2 2
93 3 3
104 4 4
115 5 5
126 6 6
137 7 7
145
1531 41 59
1626 53 58
1797 93 23
1884 62 64
1933 83 27
200
样例输出
1Case 1: maximum height = 40
2Case 2: maximum height = 21
3Case 3: maximum height = 28
4Case 4: maximum height = 342
提示
无
思路
代码
1using namespace std;
2typedef long long ll;
3const int N = 2e2 + 5;
4
5struct sc {
6 int l, w, h;
7} a[N];
8
9int f[N] = {0};
10int n, num = 1;
11
12bool cmp(sc x, sc y) {
13 if (x.l != y.l)
14 return x.l < y.l;
15 return x.w < y.w;
16}
17
18int main(void) {
19 while(~scanf("%d", &n) && n) {
20 int l, w, h, t = 0;
21 for(int i=0; i<n; i++) {
22 scanf("%d%d%d", &l, &w, &h);
23 a[t].l=l, a[t].w=w, a[t++].h=h;
24 a[t].l=l, a[t].w=h, a[t++].h=w;
25 a[t].l=w, a[t].w=l, a[t++].h=h;
26 a[t].l=w, a[t].w=h, a[t++].h=l;
27 a[t].l=h, a[t].w=l, a[t++].h=w;
28 a[t].l=h, a[t].w=w, a[t++].h=l;
29 }
30 sort(a, a+t, cmp);
31
32 int ans=0;
33 for(int i=0; i<t; i++) {
34 int mx=0;
35 for(int j=0; j<i; j++) {
36 if(a[j].l<a[i].l && a[j].w<a[i].w)
37 mx = max(mx, f[j]);
38 }
39 f[i] = a[i].h+mx;
40 ans = max(ans, f[i]);
41 }
42 printf("Case %d: maximum height = %d\n", num++, ans);
43 }
44
45 return 0;
46}