题目:
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
题意:输入一个数,然后输出几组数据,以三个为一组,是长方体的长宽高,无顺序,每一组数据的长方体都有无数个,一组数据假设2,3,6。。那么可以是2,3为长宽,亦可以2,6.。也可以3,6.。然后把长方体堆起来放,上面的长方体必须长和宽都小于下面的,求最大高度。。
想法:
可以先根据长或宽排序,然后求最长子序列。。
代码:
#include <iostream>
#include <algorithm>
using namespace std;
struct box1
{
int a,b,c;
int d;
};
bool cmp(box1 ss1,box1 ss2)
{
if(ss1.a<ss2.a) return 1;
else if(ss1.a==ss2.a&&ss1.b<ss2.b) return 1;
else return 0;
}
int inline max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int i,n,v=0;
box1 bo1[10001],bo2[10001];
//freopen("r.txt","r",stdin);
while(cin>>n&&n!=0)
{
v=v+1;
for(i=1;i<=n;i++)
{
cin>>bo1[i].a>>bo1[i].b>>bo1[i].c;
bo2[i].a=bo1[i].a;
bo2[i].b=bo1[i].b;
bo2[i].c=bo1[i].c;
bo2[i].d=bo1[i].c;
bo2[i+n].a=bo1[i].a;
bo2[i+n].b=bo1[i].c;
bo2[i+n].c=bo1[i].b;
bo2[i+n].d=bo1[i].b;
bo2[i+2*n].a=bo1[i].b;
bo2[i+2*n].b=bo1[i].c;
bo2[i+2*n].c=bo1[i].a;
bo2[i+2*n].d=bo1[i].a;
}
for(i=1;i<=3*n;i++)
{
int a;
if(bo2[i].a>bo2[i].b)
{
a=bo2[i].a;
bo2[i].a=bo2[i].b;
bo2[i].b=a;
}
}
sort(bo2+1,bo2+3*n+1,cmp);
int maxh=0;
for(i=2;i<=3*n;i++)
{
for(int j=0;j<i;j++)
if(bo2[i].a>bo2[j].a&&bo2[i].b>bo2[j].b)
bo2[i].d=max(bo2[j].d+bo2[i].c,bo2[i].d);
if(bo2[i].d>maxh) maxh=bo2[i].d;
}
cout<<"Case "<<v<<": maximum height = "<<maxh<<endl;
}
return 0;
}
做了好长时间。。