poj2392(多重背包dp,可行性类型O(nm)复杂度的解法。)

/*
translation:
	有k种石块,每种石块有ci个,每种石块所在的高度不能超过ai,求用这k种石头所能达到的最高高度

solution:
	多重背包dp可行性解法,贪心
	先排序,然后按照多重背包dp可行性解法即可ac

note:
	此题用常规的多重背包wa了,不知道为啥?

date:
	2016.9.8
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 400 +5;

struct Stock {
	int h, a, c;	//石块的高度,高度上限,该类石块的数量
	bool operator < (const Stock& rhs) const {
		if(a == rhs.a)	return h > rhs.h;
		else			return a < rhs.a;
	}
} stocks[60000];
int k;
int dp[40000 + 5];

int main()
{
	//freopen("in.txt", "r", stdin);
    while(~scanf("%d" ,&k)) {
		int max_h = -1;
		for(int i = 0; i < k; i++) {
			scanf("%d%d%d", &stocks[i].h, &stocks[i].a, &stocks[i].c);
			max_h = max(max_h, stocks[i].a);
		}

		sort(stocks, stocks + k);
		memset(dp, -1, sizeof(dp));
		dp[0] = 0;

		for(int i = 0; i < k; i++) {
			for(int j = 0; j <= stocks[i].a; j++) {
				if(dp[j] >= 0) {
					dp[j] = stocks[i].c;
				} else if(j < stocks[i].h || dp[j - stocks[i].h] <= 0) {
					dp[j] = -1;
				} else {
					dp[j] = dp[j - stocks[i].h] - 1;
				}
			}
		}

		int ans;
		for(int i = 0; i <= stocks[k - 1].a; i++)
			if(dp[i] >= 0)	ans = i;
		printf("%d\n", ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值