Test Instructions:
Given some cubes, for two cubes, only one of the two sides of the bottom is strictly less than the other one can be placed above it. The maximum height that can be obtained.
Ideas:
A cube can be turned into 6 different rectangles with different weights (heights) on the bottom, and then the nesting of these rectangles. It used to turn the problem into the longest way on a DAG map, and this time the rectangles are sorted by the bottom area from small to large, and then the problem becomes the longest ascending subsequence.
The code is as follows:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm>using namespace Std;int k;typedef struct{int x, y, Z;} S S St[200];int CMP (s s1,s s2) {return s1.x*s1.y>s2.x*s2.y;} int main () {int n,case=0; while (scanf ("%d", &n) &&n) {k=1; for (int i=0;i<n;i++) {int x, y, Z; scanf ("%d%d%d", &x,&y,&z); St[k].x=x; St[k].y=y; St[k].z=z; k++; St[k].x=y; St[k].y=x; St[k].z=z; k++; St[k].x=z; St[k].y=x; St[k].z=y; k++; St[k].x=x; St[k].y=z; St[k].z=y; k++; St[k].x=z; St[k].y=y; St[k].z=x; k++; St[k].x=y; St[k].y=z; St[k].z=x; k++; } sort (st+1,st+k,cmp); int dp[200],ans=0; for (int i=1;i<k;i++) dp[i]=st[i].z; for (int i=1;i<k;i++) for (int j=1;j<i;j++) {if (st[i].x< st[j].x&&st[i].y<st[j].y) {Dp[i]=max (dp[i],dp[j]+st[i].z); Ans=max (Ans,dp[i]); }} printf ("Case%d:maximum height =%d\n", ++case,ans); } return 0;}
Uva--437the Tower of BABYLON+DP