Monkey and banana
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 12865 accepted submission (s): 6757
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 input110 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 270
Sample outputcase 1: maximum height = 40 Case 2: maximum height = 21 case 3: maximum height = 28 case 4: maximum height = 342
Sourceuniversity of Ulm Local contest 1996
Recommend
#include<iostream>#include<stdio.h>#include<algorithm>using namespace std;const int maxn = 30*4;int num=0;struct Node{ int len,wid; int hei;} rect[maxn];int dp[maxn];void add(int t[],int sta){ if(sta==0) { rect[num].len=max(t[1],t[2]); rect[num].wid= min(t[1],t[2]); rect[num].hei=t[0]; num++; return; } if(sta==1) { rect[num].len=max(t[0],t[2]); rect[num].wid= min(t[0],t[2]); rect[num].hei=t[1]; num++; return; } rect[num].len=max(t[0],t[1]); rect[num].wid= min(t[0],t[1]); rect[num].hei=t[2]; num++;}bool cmp(Node a,Node b){ if(a.len!=b.len) return a.len<b.len; if(a.wid!=b.wid) return a.wid<b.wid; return a.hei>b.hei;}int main(){ int n; int cas=1; while(~scanf("%d",&n)&&n) { num=0; for(int i=0;i<n;i++) { int t[4]; for(int k=0;k<3;k++) scanf("%d",t+k); for(int k=0;k<3;k++) add(t,k); } int maxx = -1; sort(rect,rect+num,cmp); for(int i=0;i<num;i++) dp[i]=rect[i].hei; for(int i=1;i<num;i++) { for(int j=i-1;j>=0;j--) { if(rect[i].len>rect[j].len&&rect[i].wid>rect[j].wid) { dp[i]=max(dp[i],dp[j]+rect[i].hei); } } maxx=max(dp[i],maxx); } printf("Case %d: maximum height = %d\n",cas++,maxx); } return 0;}
View code
DP [I] indicates the maximum height of an I-based box.
When determining the I, since the box has been according to the base dimensions (that is, the length and width) increased from small to large, so the I box can only be placed in a previous I-1 under a box
(Here is what the DP array loop does)
In this way, we record the maximum value each time from the first box (we thought the first box would not be able to store other boxes, so we started directly from the second box,
Finally, output the data in the sample format.
HDU 1069 monkey and banana