时间:2016-03-27 15:19:40 星期日
题目编号:[2016-03-30][HDU][1069][Monkey and Banana]
题目大意:给定n种积木无限个,问这些积木最大能叠多高,上面的积木长宽必须严格小于下面的积木
分析:
- dp[i]表示第i个积木在顶部时候的最大高度,那么dp[i] = max(dp[i],dp[j] + h[i]);∀ji能放在j上面
- 初始条件就是长宽最大的高度是它自己,
#include <algorithm>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;int dp[90*90];struct mRect{ int x,y,z; mRect(int a = 0,int b = 0,int c = 0):x(a),y(b),z(c){}; bool operator < (const mRect & a)const{ return x > a.x || (x == a.x && y > a.y) || (x == a.x && y == a.y && z > a.z); } bool operator == (const mRect & a)const{ return x == a.x && y == a.y && z == a.z; }}r[90 * 90];int main(){ int n,cntcase = 0; while(~scanf("%d",&n) && n){ int a[3],cur = 1; for(int i = 0 ;i < n ; ++i){ for(int j = 0;j < 3 ;++j) scanf("%d",&a[j]); sort(a,a+3); r[cur++] = mRect(a[0],a[1],a[2]); r[cur++] = mRect(a[0],a[2],a[1]); r[cur++] = mRect(a[1],a[2],a[0]); } sort(r+1,r+cur); cur = unique(r+1,r+cur) - r; memset(dp,0,sizeof(dp)); r[0] = mRect(0x3f3f3f3f,0x3f3f3f3f,0); int maxh = 0; for(int i = 1;i < cur;++i){ for(int j = 0;j < i;++j){ if(r[i].x < r[j].x && r[i].y < r[j].y){ dp[i] = max(dp[i],dp[j] + r[i].z); } } maxh = max(maxh,dp[i]); } printf("Case %d: maximum height = %d\n",++cntcase,maxh); } return 0;}