hdu1069Monkey and Banana(动态规划)

Monkey and Banana

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20500    Accepted Submission(s): 10969


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

题意:给出n种矩形积木,和积木的长宽高三个属性值,这个值每个值都可以表示长宽高中一种,例如1 2 3就有6种组合123   132  213  231  312  321

要求输出他们堆起来能达到的最大高度(上面积木的底座要小于下面积木的底座)

用动态规划做

因为和他们的底座面积有关,每种积木最多有3种底座面积,可以根据2条边长排序(也相当于根据底座面积排序),排成最大值,最后变成求最大子列和。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int dp[200];
 4 struct node
 5 {
 6     int x,y,z;
 7     
 8 }a[200];
 9 bool cmp(node a,node b)//x升序  x相等  y升序  
10 {
11     if(a.x!=b.x)
12     {
13         return a.x<b.x;
14     }
15     else
16     {
17         return a.y<=b.y;
18     }
19 }
20 
21 int main() {
22     int n;int cases=0;
23     while(~scanf("%d",&n),n)
24     {
25         cases++;
26         int num=0;
27         for(int i=0;i<n;i++)
28         {
29             int x,y,z;
30             scanf("%d %d %d",&x,&y,&z);
31 //            if(x==y&&y==z)
32 //            {
33 //                a[num].x=x;
34 //                a[num].y=x;
35 //                a[num].z=x;
36 //                num++;
37 //            }
38 //            else
39 //            {
40                 a[num].x=x;
41                 a[num].y=y;
42                 a[num].z=z;
43                 num++;
44                 a[num].x=x;
45                 a[num].y=z;
46                 a[num].z=y;
47                 num++;
48                 a[num].x=y;
49                 a[num].y=x;
50                 a[num].z=z;
51                 num++;
52                 a[num].x=y;
53                 a[num].y=z;
54                 a[num].z=x;
55                 num++;
56                 a[num].x=z;
57                 a[num].y=x;
58                 a[num].z=y;
59                 num++;
60                 a[num].x=z;
61                 a[num].y=y;
62                 a[num].z=x;
63                 num++;
64         //    }
65             
66         }
67         memset(dp,0,sizeof(dp));
68         sort(a,a+num,cmp);
69         int maxx=0;
70         dp[0]=a[0].z;
71         for(int i=1;i<num;i++)//每次都和前面的比    摆放要求上小下大,这边是从上往下找的,先最小的(最上面),再大的 
72         {
73             int temp=0;//记录前面几个里面高度最大的那个  因为每次都要最优,那么就要把他放在已有的高度最高那 
74             for(int j=i-1;j>=0;j--)
75             {
76                 if(a[i].x>a[j].x&&a[i].y>a[j].y&&dp[j]>temp)//因为排过序,所以大的在后,小的在前 
77                 {
78                     temp=dp[j];
79                      
80                 }
81                 
82             }
83             dp[i]=temp+a[i].z;//i位置处的高度 
84             maxx=max(dp[i],maxx);//所求的答案 
85         }
86         printf("Case %d: maximum height = %d
",cases,maxx);
87     }
88     return 0;
89 }
原文地址:https://www.cnblogs.com/fqfzs/p/9832165.html