HDU 1069 Monkey and Banana(DP)
2016-03-07 21:11
316 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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".
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
首先,这道题的dp思路很简单,初学者也一定会做。这道题的难点在于状态的拓展——从原来的n种状态拓展到3n种状态。
在这道题里,我犯了两个错误,并且浪费了我很多时间:
1.是对排序的机理理解不透彻。任何排序规则,都必须遵循一条规则:关系可传递性(即若a大于b , b大于c,则a大于c)。我在做这道题的时候一开始用了严格判断的排序,而这种排序是不具备可传递性的 严格地说,这种不可传递性是因为判断本身的缺陷所致,有能放2种加上不能放一种,一共三种情况)。所以,正确的排序方法应该是以长优先排序。当然,网上说以宽优先,甚至以面积优先都是可行的,因为只要保证前面的元素不能放到后面元素的上面,具体能不能放需要动态规划时判断)。
2.是最后输出结果的问题,我错误的输出了最后一个元素。当然,如果在状态转移过程中没有判断,输出最后一个元素是可行的,但是在这里就不行了。
最后贴代码,这道题写了三天,有题目难的缘故,也有自己熬夜导致精神不好的缘故。以后不再熬夜了
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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".
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
Case 1: maximum height = 40 Case 2: maximum height = 21 Case 3: maximum height = 28 Case 4: maximum height = 342
首先,这道题的dp思路很简单,初学者也一定会做。这道题的难点在于状态的拓展——从原来的n种状态拓展到3n种状态。
在这道题里,我犯了两个错误,并且浪费了我很多时间:
1.是对排序的机理理解不透彻。任何排序规则,都必须遵循一条规则:关系可传递性(即若a大于b , b大于c,则a大于c)。我在做这道题的时候一开始用了严格判断的排序,而这种排序是不具备可传递性的 严格地说,这种不可传递性是因为判断本身的缺陷所致,有能放2种加上不能放一种,一共三种情况)。所以,正确的排序方法应该是以长优先排序。当然,网上说以宽优先,甚至以面积优先都是可行的,因为只要保证前面的元素不能放到后面元素的上面,具体能不能放需要动态规划时判断)。
2.是最后输出结果的问题,我错误的输出了最后一个元素。当然,如果在状态转移过程中没有判断,输出最后一个元素是可行的,但是在这里就不行了。
最后贴代码,这道题写了三天,有题目难的缘故,也有自己熬夜导致精神不好的缘故。以后不再熬夜了
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; struct myk{ int chang; int kuan; int gao;}; myk a[100],b[300]; int dp[300],i,j,m,n; void tuozhan(void) { for(j=0;j<n;j++) { b[j].chang=max(a[j].chang,a[j].kuan); b[j].kuan=min(a[j].chang,a[j].kuan); b[j].gao=a[j].gao; } for(j=n;j<2*n;j++) { b[j].gao=a[j-n].chang; b[j].chang=max(a[j-n].kuan,a[j-n].gao); b[j].kuan=min(a[j-n].kuan,a[j-n].gao); } for(j=2*n;j<3*n;j++) { b[j].gao=a[j-2*n].kuan; b[j].chang=max(a[j-2*n].chang,a[j-2*n].gao); b[j].kuan=min(a[j-2*n].chang,a[j-2*n].gao); } } bool cmp(myk x,myk y) { if(x.chang==y.chang) return(x.kuan>y.kuan); else return(x.chang>y.chang); } bool panduan(myk i,myk j) { if((i.chang<j.chang)&&(i.kuan<j.kuan)) return true; else return false; } int main(void) { int maxn,num=0; while(1) { scanf("%d",&n); if(n==0) break; for(i=0;i<n;i++) scanf("%d%d%d",&a[i].chang,&a[i].kuan,&a[i].gao); tuozhan(); sort(b,b+3*n,cmp); dp[0]=b[0].gao; for(i=1;i<3*n;i++) { maxn=0; for(j=0;j<i;j++) if(panduan(b[i],b[j])) maxn=(dp[j]>maxn)?dp[j]:maxn; dp[i]=b[i].gao+maxn; } maxn=-1; for(i=0;i<3*n;i++) maxn=(dp[i]>maxn)?dp[i]:maxn; printf("Case %d: maximum height = %d\n",++num,maxn); } return 0; }
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1002
- POJ 2635 The Embarrassed Cryptographe
- POJ 3292 Semi-prime H-numbers
- POJ 2773 HAPPY 2006
- POJ 3090 Visible Lattice Points