poj 2241 叠方块 基本动态规划

题意:给定n种block,每种block有无限多个,每个block有x, y, z三个属性

要求叠起来,使得在满足下面的长和宽严格大于上面的,情况下,高度最高


解法:

d[i] 表示 以第i个物品为能达到的最大高度

转移方程 d[i] = max{ d[i] ,  d[x] + height(x) 其中x要满足题目要求约束,遍取 0 - n - 1}

max(d[x])就是答案

#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;

///宏定义
const int  INF = 990000000;
const int maxn = 500 ;
const int MAXN = maxn;
///全局变量 和 函数
//int T;
int max(int a, int b)
{
	return a > b ? a : b;
}

int n;
struct BLOCK
{
	int x, y, z;
	bool operator < (const BLOCK& t) const
	{
		return (x * y) > (t.x * t.y);
	}
};
BLOCK blocks[maxn * 5];
int d[maxn * 5];
bool vis[maxn * 5];
int cnt;
int dp(int k)
{
	if (vis[k])
		return d[k];
	vis[k] = true;
	int i, j;
	int nowy, nowx, mh, maxheight;
	//以x, y为底

	mh = blocks[k].z;
	nowy = blocks[k].y;
	nowx = blocks[k].x;
	maxheight = mh;
	for (i = cnt - 1; i >= 0; i--)
	{
		if ( (nowy < blocks[i].y && nowx < blocks[i].x) || (nowx < blocks[i].y && nowy < blocks[i].x) ) //写错了好几次
		{
			maxheight = max(maxheight, dp(i) + mh);
		}
	}
	return d[k] = maxheight;
}
int main()
{
	///变量定义
	int i, j;	
	int cases = 1;
	while(1)
	{
		memset(vis, false, sizeof(vis));
		scanf("%d", &n);
		if (n == 0)
			break;
		cnt = 0;
		for (i = 0; i < n; i++)
		{
			int x, y, z;
			scanf("%d %d %d", &x, &y, &z);

			blocks[cnt].x = x;
			blocks[cnt].y = y;
			blocks[cnt].z = z;
			cnt++;

			blocks[cnt].x = x;
			blocks[cnt].y = z;
			blocks[cnt].z = y;
			cnt++;

			blocks[cnt].x = y;
			blocks[cnt].y = z;
			blocks[cnt].z = x;
			cnt++;
		}
//		sort(blocks, blocks + cnt);
//		vis[0] = true;
//		d[0] = blocks[0].z;
		int ans = -1;
		for (i = cnt - 1; i >= 0; i--)
		{
			ans = max(ans, dp(i));
		}
		printf("Case %d: maximum height = %d\n", cases++, ans);
	}
	
	///结束
	return 0;
}



在此基础上上更改一下细节处理,对于block的面积从大到小进行排序

那么d[i] = max{ d[i],  d[k]  + height[k]  k < i }

速度会更快

#include <iostream>
#include <vector>
#include <map>
#include <list>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <cstring>
#include <queue>
using namespace std;

///宏定义
const int  INF = 990000000;
const int maxn = 500 ;
const int MAXN = maxn;
///全局变量 和 函数
//int T;
int max(int a, int b)
{
	return a > b ? a : b;
}

int n;
struct BLOCK
{
	int x, y, z;
	bool operator < (const BLOCK& t) const
	{
		return (x * y) > (t.x * t.y);
	}
};
BLOCK blocks[maxn * 5];
int d[maxn * 5];
bool vis[maxn * 5];
int cnt;
int dp(int k)
{
	if (vis[k])
		return d[k];
	vis[k] = true;
	int i, j;
	int nowy, nowx, mh, maxheight;
	//以x, y为底

	mh = blocks[k].z;
	nowy = blocks[k].y;
	nowx = blocks[k].x;
	maxheight = mh;
	for (i = k - 1; i >= 0; i--)
	{
		if ( (nowy < blocks[i].y && nowx < blocks[i].x) || (nowx < blocks[i].y && nowy < blocks[i].x) ) //写错了好几次
		{
			maxheight = max(maxheight, dp(i) + mh);
		}
	}
	return d[k] = maxheight;
}
int main()
{
	///变量定义
	int i, j;	
	int cases = 1;
	while(1)
	{
		memset(vis, false, sizeof(vis));
		scanf("%d", &n);
		if (n == 0)
			break;
		cnt = 0;
		for (i = 0; i < n; i++)
		{
			int x, y, z;
			scanf("%d %d %d", &x, &y, &z);

			blocks[cnt].x = x;
			blocks[cnt].y = y;
			blocks[cnt].z = z;
			cnt++;

			blocks[cnt].x = x;
			blocks[cnt].y = z;
			blocks[cnt].z = y;
			cnt++;

			blocks[cnt].x = y;
			blocks[cnt].y = z;
			blocks[cnt].z = x;
			cnt++;
		}
		sort(blocks, blocks + cnt); //先进行排序
		vis[0] = true;              //面积最大的作为顶的最大高度,记忆化搜索
		d[0] = blocks[0].z;
		int ans = -1;
		for (i = cnt - 1; i >= 0; i--)
		{
			ans = max(ans, dp(i));
		}
		printf("Case %d: maximum height = %d\n", cases++, ans);
	}
	
	///结束
	return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值