杭电1069:http://acm.hdu.edu.cn/showproblem.php?pid=1069
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.
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.
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
Case 1: maximum height = 40 Case 2: maximum height = 21 Case 3: maximum height = 28 Case 4: maximum height = 342题意大概是用给定的长方体搭个小塔把!!(同一大小的长方体个数不限,所以一个长方体有三种摆法,得到三种不同的高) 要求是下面长方体的长和宽要大于上面一层的。求小塔的总高度。代码:#include<iostream> #include<stdio.h> #include<algorithm> using namespace std;
struct node { int a,b,c; }q[108];
bool cmp(node d,node e ) { return e.a<d.a; }//布尔类型排序
int main() { int n,m,i,j,r=1; int x,y,z,max1,dp[108]={0}; while(cin>>n && n != 0) { max1=0; for(i=0,j=0;j<n;j++) { scanf("%d%d%d",&x,&y,&z); q[i].a=min(x,y);//三种不同的形态,对应的高度 q[i].b=max(x,y); q[i].c=z; q[i+1].a=min(x,z); q[i+1].b=max(x,z); q[i+1].c=y; q[i+2].a=min(y,z); q[i+2].b=max(y,z); q[i+2].c=x; i+=3; }
sort(q,q+3*n,cmp);
for(i=0;i<3*n;i++)//动规求 最大值 { dp[i]=q[i].c; for(j=i-1;j>=0;j--) { if(q[j].a>q[i].a && q[j].b>q[i].b) { if(dp[j]+q[i].c>dp[i]) dp[i]=q[i].c+dp[j]; } }//cout<<dp[i]<<endl;(排序的时候排反了,在这里加了个输出,用来检查错误) if(dp[i]>max1) max1=dp[i]; } printf("Case %d: maximum height = %d\n",r++,max1); } return 0; }
用排序的时候几次都把顺序搞反了,以后要注意。。。。动规路走起。。。。。