Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1069
Monkey and banana
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 7397 accepted submission (s): 3801
Problem descriptiona 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 cocould 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 cocould 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.
Inputthe input file will contain in 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.
Outputfor 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: 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
Understanding and analysis of the meaning of the question: each cube can have three different placement methods, just as the three sides of the input have three arrangement methods of length, width, and height, because the number is not limited, strictly decreasing (increasing) so we can save all three sides and sort them. In fact, it is a little difficult to think about it at the beginning. I think it is quite interesting to remember it. AC code:
# Include <iostream> # include <cstring> # include <cstdio> # include <algorithm> using namespace STD; struct node {int X, Y, Z, H ;} s [100]; int N, DP [100]; bool CMP (node A, Node B) {if (. X = B. x) return. y> B. y; return. x> B. x;} int dp () {sort (S, S + N, CMP); int maxh = 0; For (INT I = 0; I <n; I ++) {DP [I] = s [I]. z; For (Int J = I-1; j> = 0; j --) if (s [J]. x> S [I]. X & S [J]. y> S [I]. y) // strictly decrease if (DP [J] + s [I]. z> DP [I]) DP [I] = s [I]. Z + dp [J]; If (DP [I]> MAX H) maxh = DP [I];} return maxh;} int main () {int I, j, ant = 1, A, B, C; while (~ Scanf ("% d", & N), n) {for (I = 0, j = 0; j <n; j ++) {scanf ("% d", & A, & B, & C ); // The following three cases are the length, width, and height of the three cubes. S [I]. X = min (a, B); // width s [I]. y = max (a, B); // long s [I]. z = C; s [I + 1]. X = min (a, c); s [I + 1]. y = max (a, c); s [I + 1]. z = B; s [I + 2]. X = min (B, c); s [I + 2]. y = max (B, c); s [I + 2]. z = A; I + = 3;} n = I; printf ("case % d: maximum height = % d \ n", ant ++, dp ());} return 0 ;}