hdu1069 Monkey and Banana dp

来源:互联网 发布:数据库建模培训 编辑:程序博客网 时间:2024/05/18 03:47

题目链接:http://hdu.hustoj.com/showproblem.php?pid=1069

                                                                                                                  

Monkey and Banana

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


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
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


题意:

一道经典的DP题目,类似于求最长递增子序列吧。

一堆科学家研究猩猩的智商,给他M种长方体,每种N个。

然后,将一个香蕉挂在屋顶,让猩猩通过 叠长方体来够到香蕉。

现在给你M种长方体,计算,最高能堆多高。

要求位于上面的长方体的长要大于(注意不是大于等于)下面长方体的长,上面长方体的宽大于下面长方体的宽。

第i块砖块的长宽高分别用xi,yi,zi来表示。 同时,由于砖块是可以旋转的,每个砖块的3条边可以组成6种不同的长宽高。

 
当且仅当A砖块的长和宽都分别小于B砖块的长和宽时,A砖块才能放到B砖块的上面,因为必须留有一些空间让猴子来踩。

解题:

一个长方体,可以有6种不同的摆法。

因为数据中 长方体种类最多30种,也就是说数组最大可以开到 30*6=180 完全可以

对180个格子对X从小到大排序,X相等,Y就重小到大排序,那么这个问题就可以转换成类似求最大递增子序列问题一样思路的DP,DP[i]表示第i个格子时的最大值,dp[i+1]就是从前i个中找符合条件的最大的一个加上去,因为,必须X越来越小,反过来就是X越来越大

然后用dp[i]来存,到第i个木块,最高可以累多高。

动态转移方程:dp[i] = max(dp[i], dp[j] + G[i].z);

代码:

#define _CRT_SBCURE_MO_DEPRECATE  #include<iostream>  #include<stdlib.h>  #include<stdio.h>  #include<cmath>  #include<algorithm>  #include<string>  #include<string.h>  #include<set>  #include<queue>  #include<stack>  #include<functional>   using namespace std;const int maxn = 200 + 10;const int INF = 0x3f3f3f3f;struct node {int x, y, z;};int dp[maxn];node G[maxn];int n;int k, cnt;int sum;int s[3];bool cmp(node x, node y) {if (x.x != y.x) return x.x > y.x;return x.y > y.y;}int main(){k = 0;while (scanf("%d", &n)!= EOF && n!=0) {cnt = 0;G[0].x = G[0].y = INF;for (int i = 0; i < n; i++){scanf("%d %d %d", &s[0], &s[1], &s[2]);sort(s, s + 3);G[++cnt].x = s[0]; G[cnt].y = s[1]; G[cnt].z = s[2];G[++cnt].x = s[1]; G[cnt].y = s[2]; G[cnt].z = s[0];G[++cnt].x = s[0]; G[cnt].y = s[2]; G[cnt].z = s[1];}sort(G+1, G + cnt +1, cmp);memset(dp, 0, sizeof(dp));for (int i = 1; i <= cnt; i++) {for (int j = 0; j < i; j++) {if (G[i].x < G[j].x && G[i].y < G[j].y)dp[i] = max(dp[i], dp[j] + G[i].z);}}sum = 0;for (int i = 1; i <= cnt; i++) {sum = max(sum, dp[i]);}printf("Case %d: maximum height = %d\n", ++k, sum);}system("pause");return 0;}


0 0
原创粉丝点击