题意:
有n个,长x宽y高z的长方体,把这些长方体摞起来,上面长方体底面的长宽一定要小于下面的,求能摞的最大高度。
分析:
一个长方体,可以有三种放法,先把所有放的状态存起来,按底面升序排列,dp[i]前i个能构成的最大高度,dp[i]=max(dp[i],dp[j]+h) h为当前长方体高度
#include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <string> #include <cctype> #include <complex> #include <cassert> #include <utility> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; typedef pair<int,int> PII; typedef long long ll; #define lson l,m,rt<<1 #define rson m+1,r,rt<<11 #define All 1,N,1 #define read freopen("in.txt", "r", stdin) const ll INFll = 0x3f3f3f3f3f3f3f3fLL; const int INF= 0x7ffffff; const int maxn = 1e5+100; const int mod = 1000000007; struct Block{ int x; int y; int z; }b[210]; bool cmp(Block u,Block v){ if(u.x==v.x)return u.y>v.y; else return u.x>v.x; } int dp[210],n; int solve(int num){ memset(dp,0,sizeof(dp)); sort(b,b+num,cmp); int ma=-1; for(int i=0;i<num;++i){ dp[i]=b[i].z; for(int j=0;j<i;++j){ if(b[j].y>b[i].y&&b[j].x>b[i].x) dp[i]=max(dp[i],dp[j]+b[i].z); } if(dp[i]>ma) ma=dp[i]; } return ma; } int main() {int t=0,xx,yy,zz; while(~scanf("%d",&n)){ if(n==0)break; t++; int num=0; for(int i=0;i<n;++i){ scanf("%d%d%d",&xx,&yy,&zz); b[num].x=xx;b[num].y=yy;b[num].z=zz; num++; b[num].x=yy;b[num].y=xx;b[num].z=zz; num++; b[num].x=zz;b[num].y=yy;b[num].z=xx; num++; b[num].x=yy;b[num].y=zz;b[num].z=xx; num++; b[num].x=xx;b[num].y=zz;b[num].z=yy; num++; b[num].x=zz;b[num].y=xx;b[num].z=yy; num++; } /* for(int i=0;i<num;++i) printf("%d %d %d\n",b[i].x,b[i].y,b[i].z);*/ printf("Case %d: maximum height = %d\n",t,solve(num)); } return 0; }
时间: 2024-10-20 01:23:51