Problem E
Time Limit : 2000/1000ms (Java/Other) Memory Limit :65536/32768K (Java/Other)
Problem Description
A group ofresearchers are designing an experiment to test the IQ of a monkey. They willhang a banana at the roof of a building, and at the mean time, provide themonkey with some blocks. If the monkey is clever enough, it shall be able toreach the banana by placing one block on the top another to build a tower andclimb up to get its favorite food.<br><br>The researchers have ntypes of blocks, and an unlimited supply of blocks of each type. Each type-iblock was a rectangular solid with linear dimensions (xi, yi, zi). A blockcould be reoriented so that any two of its three dimensions determined the dimensionsof the base and the other dimension was the height. <br><br>Theywant to make sure that the tallest tower possible by stacking blocks can reachthe roof. The problem is that, in building a tower, one block could only beplaced on top of another block as long as the two base dimensions of the upperblock were both strictly smaller than the corresponding base dimensions of thelower block because there has to be some space for the monkey to step on. Thismeant, for example, that blocks oriented to have equal-sized bases couldn't bestacked. <br><br>Your job is to write a program that determines theheight of the tallest tower the monkey can build with a given set ofblocks.<br>
Input
The input filewill contain one or more test cases. The first line of each test case containsan integer n,<br>representing the number of different blocks in thefollowing data set. The maximum value for n is 30.<br>Each of the next nlines contains three integers representing the values xi, yi and zi.<br>Inputis terminated by a value of zero (0) for n.<br>
Output
For each testcase, print one line containing the case number (they are numbered sequentiallystarting from 1) and the height of the tallest possible tower in the format"Case case: maximum height = height".<br>
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: maximumheight = 40
Case 2: maximumheight = 21
Case 3: maximumheight = 28
Case 4: maximumheight = 342
题意:类似于求最长递增子序列,给出m(m<=30)种每种n(n>=0)个长方体,求堆起来的最大高度。(要求位于上面的长方体的长要大于下面长方体的长,上面长方体的宽大于下面长方体的宽)
思路:按面积排序,长方体状态最多有180个,从而这个问题就可以转换成类似求最大递增子序列问题一样思路的DP。DP[i]表示第i个格子时的最大值,dp[i+1]就是从前i个中找符合条件的最大的一个加上去。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct point
{
int x;
int y;
int z;
}dp[200];
int cmp(const void *a,const void *b)
{
struct point *c=(point *)a;
struct point *d=(point *)b;
if(c->x!=d->x)
return c->x-d->x;
else
return
c->y-d->y;
}
int main()
{
int t,k=1,i,j,len;
int temp;
int maxn;
int xx,yy,zz;
while(scanf("%d",&t),t)
{
len=0;
while(t--)
{
scanf("%d%d%d",&xx,&yy,&zz);
dp[len].x=xx;
dp[len].y=yy;
dp[len].z=zz;
++len;
dp[len].x=yy;
dp[len].y=zz;
dp[len].z=xx;
++len;
dp[len].x=zz;
dp[len].y=xx;
dp[len].z=yy;
++len;
dp[len].x=yy;
dp[len].y=xx;
dp[len].z=zz;
++len;
dp[len].x=zz;
dp[len].y=yy;
dp[len].z=xx;
++len;
dp[len].x=xx;
dp[len].y=zz;
dp[len].z=yy;
++len;
}
qsort(dp,len,sizeof(dp[0]),cmp);
for(i=1;i<len;i++)
{
temp=0;
for(j=0;j<i;j++)
{
if(((dp[j].x<dp[i].x&&dp[j].y<dp[i].y)||(dp[j].x<dp[j].y&&dp[j].y<dp[j].x))&&dp[j].z>temp)
temp=dp[j].z;
}
dp[i].z+=temp;
}
maxn=0;
for(i=0;i<len;i++)
if(dp[i].z>maxn)
maxn=dp[i].z;
printf("Case %d: maximum height = %d\n",k++,maxn);
}
return 0;
}