There are 3 ways to stack each box, so there are 3*n boxes. The boxes are sorted by length from large to small, and the sum of the height of the longest descending sub-sequence of the box by width is obtained.
#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;#defineMAX (A, B) (a>b)? a:b#defineMIN (A, B) (a>b)? b:aConst intSize= -+6;//as large as possible;structnode{intl; intW; inth;}; Node box[3*SIZE];BOOLComp (node no1, node NO2) {returnNO1.L >NO2.L;}intMain () {intN; intt=0; while(SCANF ("%d", &n)!=eof&&n!=0) { for(intI=0; i<n;i++) { intx, Y, Z scanf (" %d%d%d",&x,&y,&z); Box[i*3+0].h=x;box[i*3+0].l=max (y,z); box[i*3+0].w=MIN (y,z); Box[i*3+1].h=y;box[i*3+1].l=max (x,z); box[i*3+1].w=MIN (x,z); Box[i*3+2].h=z;box[i*3+2].l=max (y,x); box[i*3+2].w=MIN (y,x); } sort (Box,box+3*N,comp); intans=-1; intH[size]; memset (H,0,sizeof(h)); for(intI=0;i<3*n;i++) {H[i]=box[i].h; for(intj=0; j<i;j++) { if(box[i].w<box[j].w&&box[i].l<BOX[J].L) {H[i]=max (h[j]+Box[i].h,h[i]); }} ans=MAX (H[i],ans); } printf ("Case %d:maximum height =%d\n",++T,ans); } return 0;}
Hdoj (1069) longest descent subsequence