这题目,就是排序+求最大子序列
题意:给出三个数指箱子的长宽高,但是可以调换,要求箱子摞在一起的最大高度,必须是下面的箱子的长和宽均要比上面的箱子的长和宽要大。(不能相等)。
1 /* 2 本题目基本思路已经成型,那就是先排序,将这些矩形排序,要求长,宽进行排序; 3 我的思路是这样的,先按照长度排序,排序以后要利用DP求出 以高度和为一样的最大子序列。 4 然后就好办了。然后只要相加就可以了 5 */ 6 #include <iostream> 7 #include <algorithm> 8 #include <string.h> 9 using namespace std; 10 struct abc 11 { 12 int a,b,c; //a代表长,b代表宽,c代表高 13 } cube[1000]; 14 bool cmp(abc aa,abc bb) 15 { 16 if(aa.a==bb.a) return aa.b>bb.b; 17 else return aa.a>bb.a; 18 19 } 20 void paixu(int a[]) 21 { 22 int i,j,t; 23 for(i=0; i<3; ++i) 24 for(j=i; j<3; ++j) 25 { 26 if(a[j]>a[i]) 27 { 28 t=a[i]; 29 a[i]=a[j]; 30 a[j]=t; 31 } 32 } 33 } 34 int main() 35 { 36 int n,i,j; 37 int aaa[3]; 38 int maxsum[1000]; 39 int count; 40 int sum; 41 int cas=1; 42 43 while(cin>>n&&n) 44 { 45 memset(maxsum,0,sizeof(maxsum)); 46 count=0; 47 sum=0; 48 for(i=0; i<n; ++i) 49 { 50 cin>>aaa[0]>>aaa[1]>>aaa[2]; 51 paixu(aaa); 52 cube[count].a=aaa[0]; 53 cube[count].b=aaa[1]; 54 cube[count].c=aaa[2]; 55 count++; 56 cube[count].a=aaa[1]; 57 cube[count].b=aaa[2]; 58 cube[count].c=aaa[0]; 59 count++; 60 cube[count].a=aaa[0]; 61 cube[count].b=aaa[2]; 62 cube[count].c=aaa[1]; 63 count++; 64 } 65 sort(cube,cube+count,cmp); 66 int maxx; 67 for(i=0; i<count; ++i) 68 maxsum[i]=cube[i].c; 69 for(i=0; i<count; ++i) 70 { 71 maxx=0; 72 for(j=0; j<i; ++j) 73 { 74 if(cube[j].b>cube[i].b&&cube[j].a>cube[i].a&&maxsum[j]>maxx) 75 { 76 maxx=maxsum[j]; 77 } 78 } 79 maxsum[i]=maxsum[i]+maxx; 80 } 81 sum=maxsum[0]; 82 for(i=1; i<count; ++i) 83 if(maxsum[i]>sum) 84 { 85 sum=maxsum[i]; 86 } 87 cout<<"Case "<<cas++<<": maximum height = "<<sum<<endl; 88 } 89 return 0; 90 }