POJ 2392 Space Elevator 背包题解

简介:

多重背包。本题不须要二分优化。相对简单点。由于反复数十分小,小于10。

而添加一个限制每种材料的高度做法。假设使用逆向填表,那么仅仅须要从这个高度往小递归填表就能够了。

还有就是注意要排序,以限制高度为标准从小到大排序。否则答案错误的。

#include <stdio.h>
#include <string.h>
#include <algorithm>
using std::sort;

const int MAX_K = 401;
const int MAX_H = 40001;
struct HCA
{
	int h, a, c;
	bool operator<(const HCA &hac)
	{
		return a < hac.a;
	}
};
HCA hca[MAX_K];
bool tbl[MAX_H];

inline int max(int a, int b) { return a > b ? a : b; }

int bagDP(int B)
{
	memset(tbl, 0, sizeof(tbl));
	tbl[0] = true;
	for (int i = 1; i <= B; i++)
	{
		int k = 1;
		for ( ; (k << 1) <= hca[i].c; k <<= 1)
		{
			for (int j = hca[i].a; j >= hca[i].h*k; j--)
				if (tbl[j-hca[i].h*k]) tbl[j] = true;
		}
		k = hca[i].c - k + 1;
		for (int j = hca[i].a; j >= hca[i].h*k; j--)
			if (tbl[j-hca[i].h*k]) tbl[j] = true;
	}
	int i = MAX_H - 1;
	for (; i > 0 && !tbl[i]; i--);
	return i;
}

int main()
{
	int blocks;
	scanf("%d", &blocks);
	for (int i = 1; i <= blocks; i++)
	{
		scanf("%d %d %d", &hca[i].h, &hca[i].a, &hca[i].c);
	}
	sort(hca, hca+blocks+1);
	printf("%d\n", bagDP(blocks));
	return 0;
}







本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5259833.html,如需转载请自行联系原作者

相关文章
|
10月前
|
Java
hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
35 0
|
10月前
hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】(贪心)
hdu 1052 Tian Ji -- The Horse Racing【田忌赛马】(贪心)
40 0
|
11月前
|
人工智能
poj 2299 Ultra-QuickSort 求逆序数 树状数组解法
所谓离散化,我们的了解就是对原数组排序,然后用所在位置的下标代替原数,这样我们就可以把数据范围缩小到1-500000,这个数组是开的下的。
37 0
|
11月前
poj 3624 Charm Bracelet(简单01背包)
Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.
35 0
HDU-1016,Prime Ring Problem(DFS+素数)
HDU-1016,Prime Ring Problem(DFS+素数)
|
知识图谱
POJ-1384,Piggy-Bank(完全背包)
POJ-1384,Piggy-Bank(完全背包)
HDU-1213,How Many Tables(并查集水题)
HDU-1213,How Many Tables(并查集水题)
[转]POJ3624 Charm Bracelet(典型01背包问题)
来源:https://www.cnblogs.com/jinglecjy/p/5674796.html 题目链接:http://bailian.openjudge.cn/practice/4131/ Time Limit: 1000MS          Memory Limit: 65536K  ...
1230 0