HDU 1069 Monkey and Banana

三维形体堆叠算法
本文介绍了一种求解三维形体堆叠最大高度的算法。通过预处理形体的不同放置方式并排序,利用动态规划求解最优解。具体步骤包括输入形体尺寸、生成不同放置状态、排序形体并使用动态规划算法找出最大堆叠高度。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
using namespace std;

#define inf 0x3f3f3f3f
#define eps 1e-8
#define ll long long 
#define ull unsigned long long
#define For( i, x, y ) for( int i = x; i < y; ++i )
#define mxn 10200

struct node {
	int x, y, h, s;
	bool operator < ( const node b ) const {
		return s > b.s;     //按面积排序
	}
}a[mxn];
int dp[mxn];
int main() {
	int n;
	int cas = 1;
	while( cin >> n && n ) {
		node t;
		int cnt = 0;
		For( i, 0, n ) {
			cin >> t.x >> t.y >> t.h;
			t.s = t.x * t.y;
			a[cnt++] = t;
			swap( t.x, t.h );
			t.s = t.x * t.y;
			a[cnt++] = t;
			swap( t.y, t.h );
			t.s = t.x * t.y;
			a[cnt++] = t;
		}
		sort( a, a + cnt );
		int mx = 0;
		memset( dp, 0, sizeof( dp ) );
		mx = dp[0] = a[0].h;
		For( i, 1, cnt ) {
			For( j, 0, i ) {
				if( ( a[j].x > a[i].x && a[j].y > a[i].y ) || ( a[j].y > a[i].x && a[j].x > a[i].y ) ) {
					dp[i] = max( dp[i], dp[j] + a[i].h );
				}
			}
			dp[i] = max( dp[i], a[i].h );
			mx = max( dp[i], mx );
		}
		cout << "Case " << cas++ << ": maximum height = " << mx << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值