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

Monkey and Banana

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


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
  1 #include<stdio.h>

  2 #include<algorithm>

  3 const int maxn = 200;

  4 struct Node

  5 {

  6     int x,y,z;

  7     int dp;

  8 }stick[maxn];

  9 int cmp(const struct Node a, const struct Node b)

 10 {

 11     if(a.x != b.x)

 12         return a.x < b.x;

 13     else if(a.x == b.x) return a.y < b.y;

 14     else return 0;

 15 

 16 }

 17 int main()

 18 {

 19     int n,x,y,z,k;

 20     int cnt = 1;

 21     while(~scanf("%d",&n)&&n)

 22     {

 23         k = 0;

 24         for(int i = 0; i < n; i++)

 25         {

 26             scanf("%d %d %d",&x,&y,&z);

 27             if(x == y)

 28             {

 29                 if(y == z)

 30                 {

 31                     stick[k].x = x;

 32                     stick[k].y = y;

 33                     stick[k].z = z;

 34                     stick[k].dp = stick[k].z;

 35                     k++;

 36                 }

 37                 else

 38                 {

 39                     stick[k].x = x;

 40                     stick[k].y = y;

 41                     stick[k].z = z;

 42                     stick[k].dp = stick[k].z;

 43                     k++;

 44                     stick[k].x = z;

 45                     stick[k].y = y;

 46                     stick[k].z = x;

 47                     stick[k].dp = stick[k].z;

 48                     k++;

 49                     stick[k].x = y;

 50                     stick[k].y = z;

 51                     stick[k].z = x;

 52                     stick[k].dp = stick[k].z;

 53                     k++;

 54 

 55 

 56                 }

 57             }

 58             else

 59             {

 60                 if(y == z)

 61                 {

 62                     stick[k].x = x;

 63                     stick[k].y = y;

 64                     stick[k].z = z;

 65                     stick[k].dp = stick[k].z;

 66                     k++;

 67                     stick[k].x = z;

 68                     stick[k].y = y;

 69                     stick[k].z = x;

 70                     stick[k].dp = stick[k].z;

 71                     k++;

 72                     stick[k].x = y;

 73                     stick[k].y = x;

 74                     stick[k].z = z;

 75                     stick[k].dp = stick[k].z;

 76                     k++;

 77                 }

 78                 else if(x == z)

 79                 {

 80                     stick[k].x = x;

 81                     stick[k].y = y;

 82                     stick[k].z = z;

 83                     stick[k].dp = stick[k].z;

 84                     k++;

 85                     stick[k].x = y;

 86                     stick[k].y = z;

 87                     stick[k].z = x;

 88                     stick[k].dp = stick[k].z;

 89                     k++;

 90                     stick[k].x = x;

 91                     stick[k].y = z;

 92                     stick[k].z = y;

 93                     stick[k].dp = stick[k].z;

 94                     k++;

 95                 }

 96                 else

 97                 {

 98                     stick[k].x = x;

 99                     stick[k].y = y;

100                     stick[k].z = z;

101                     stick[k].dp = stick[k].z;

102                     k++;

103                     stick[k].x = x;

104                     stick[k].y = z;

105                     stick[k].z = y;

106                     stick[k].dp = stick[k].z;

107                     k++;

108                     stick[k].x = y;

109                     stick[k].y = x;

110                     stick[k].z = z;

111                     stick[k].dp = stick[k].z;

112                     k++;

113                     stick[k].x = y;

114                     stick[k].y = z;

115                     stick[k].z = x;

116                     stick[k].dp = stick[k].z;

117                     k++;

118                     stick[k].x = z;

119                     stick[k].y = x;

120                     stick[k].z = y;

121                     stick[k].dp = stick[k].z;

122                     k++;

123                     stick[k].x = z;

124                     stick[k].y = y;

125                     stick[k].z = x;

126                     stick[k].dp = stick[k].z;

127                     k++;

128                 }

129             }

130 

131         }

132         std::sort(stick,stick+k,cmp);

133         int maxh = 0,i,j;

134         for( i = 1;i < k; i++)

135         {

136             for(j = 0; j < i; j++)

137             {

138                 if(stick[i].x > stick[j].x&&stick[i].y>stick[j].y&&

139                    stick[i].dp < stick[j].dp+stick[i].z)

140                     stick[i].dp = stick[j].dp + stick[i].z;

141             }

142             if(maxh < stick[i].dp)

143                 maxh = stick[i].dp;

144         }

145         printf("Case %d: maximum height = %d\n",cnt++,maxh);

146     }

147     return 0;

148 }

 题目:给出一些长方体,然后让你把他堆成塔,
要求下面的塔的要比上面的塔大(长和宽),
而且每一种长方体的数量都是无限的。

此题目考察到动态规划里的最长有序子序列

每输入三个数x,y,z,把他们所可能对应长宽高的情况放在数组里,这样解决了每个长方体都无限多的问题,然后转化为求最长上升子序列的问题。是一道经典dp的问题。

你可能感兴趣的:(key)