题目链接:
题意:把给定尺寸的长方体(数量不限)叠加在一起,叠加的条件是,上面一个长方体的长和宽比下面长方体的长和宽短,不能相等,长方体可以任意面朝下摆放,求这些长方体能叠加的最高的高度。
把每个长方体分成3个元素。然后就和矩形嵌套差不多了,排序之后求容量最大的子序列。
代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <ctype.h> 7 #include <iomanip> 8 #include <queue> 9 #include <stdlib.h> 10 using namespace std; 11 12 struct node 13 { 14 int x,y,h; 15 }f[10100]; 16 17 int cmp(node a,node b) 18 { 19 if(a.x==b.x) 20 return a.y<b.y; 21 return a.x<b.x; 22 } 23 24 int main() 25 { 26 int i,j,t,n,MAX,max1; 27 int a,b,c,p=1; 28 while(cin>>n&&n){ 29 t=0; 30 for(i=0;i<n;i++){ 31 cin>>a>>b>>c; 32 f[t].x=a,f[t].y=b,f[t++].h=c; 33 f[t].x=b,f[t].y=a,f[t++].h=c; 34 f[t].x=a,f[t].y=c,f[t++].h=b; 35 f[t].x=c,f[t].y=a,f[t++].h=b; 36 f[t].x=c,f[t].y=b,f[t++].h=a; 37 f[t].x=b,f[t].y=c,f[t++].h=a; 38 } 39 sort(f,f+t,cmp); 40 max1=f[0].h; 41 for(i=1;i<t;i++){ 42 MAX=0; 43 for(j=0;j<t;j++) 44 if((f[i].x>f[j].x&&f[i].y>f[j].y)||(f[i].y>f[j].x&&f[i].x>f[j].y)) 45 MAX=max(MAX,f[j].h); 46 f[i].h+=MAX; 47 max1=max(max1,f[i].h); 48 } 49 printf("Case %d: maximum height = %d ",p++,max1); 50 } 51 }