Monkey and Banana

来源:互联网 发布:携程数据分析师笔试题 编辑:程序博客网 时间:2024/04/25 07:35

/* 最大递增子序列模型 ,题目意思:给你很多种箱子的长宽高,求箱子所能垒成的最高高度,要求必须底层的箱子长宽覆盖上层的箱子,注意每种箱子都有无限多个(这个很重要) 最近听项目高手说不管写什么程序,一定要写点注释,所以我也学着写点,为以后打基础!*/

#include < iostream >#include < cstdio   >#include < cstring  >#include < algorithm>#define        N 1000using namespace  std;struct node{//x宽 y长 z高int x,y,z;}a[N],b[N];int cmp(node a1,node a2){return a1.x<a2.x;}int Max (int a1,int b1){return a1>b1?a1:b1;}/*思路:先按x的值排序,然后按照x y的值求最大的递增子序列,保证递增子序列可求的最大的height*/int main(){//freopen("t.txt","r",stdin);int i,j,k=0,n;while(scanf("%d",&n),n){printf("Case %d: ",++k);int x1,y1,z1,nt,h;memset(b,0,sizeof(b));for(nt=i=0 ; i<n ; i++){//翻转箱子,叠举6种情形scanf("%d%d%d",&x1,&y1,&z1);a[nt].x=x1,a[nt].y=y1,a[nt++].z=z1;a[nt].x=x1,a[nt].y=z1,a[nt++].z=y1;a[nt].x=y1,a[nt].y=x1,a[nt++].z=z1;a[nt].x=y1,a[nt].y=z1,a[nt++].z=x1;a[nt].x=z1,a[nt].y=x1,a[nt++].z=y1;a[nt].x=z1,a[nt].y=y1,a[nt++].z=x1;}sort(a,a+nt,cmp);//保证最大递增子序列的前提,求max(H)for(i=0 ; i<nt ;i++){b[i].z = a[i].z;for(j=0 ; j<i ;j++){if(a[i].y>a[j].y && a[i].x>a[j].x)b[i].z=Max(b[i].z,a[i].z+b[j].z);}}for(h=i=0;i<nt;i++)h=Max(b[i].z,h);printf("maximum height = %d\n", h);}return 0;}


 

原创粉丝点击