HDU3466 - Proud Merchants(贪心+01背包)

题目链接 HDU3466

【题意】有M(<=5000)元钱,在N(<=500)个物品中买到价值最高是多少,每个物品价格为wi,购买者需要拥有qi,价值为vi;

【分析】一看就是01背包,但是他加了个限制条件,购买者需要有qi元钱,这样就让每个物品购买顺序的不同而影响到01背包的结果,则需要贪心的排一下序才可以,以前做过一题价值随时间变化的01背包也需要排序,就是NYOJ747

但是这题稍微有点不同,价值不会变,只会改变购买的门槛,那么只要让购买顺序需要的空间尽量小,以留出更多的空间给后面的物品。

贪心过程:

假设A,B两个物品,要使得先后顺序让所需空间最小,才能让后面的价值可能越大
假如A先放,则需要空间 q1+w2;
B先放,则所需空间为   q2+w1;
假如要让A排在前面,则排序方式为 q1+w2<q2+w1

【AC CODE】46ms

/*
A,B两个物品,要使得先后顺序让所需空间最小,才能让后面的价值可能越大
假如A先放,则需要空间 q1+w2;
B先放,则所需空间为   q2+w1;
假如要让A排在前面,则排序方式为 q1+w2<q2+w1
*/
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <map>
//#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef long long LL;
#define rep(i,a,n) for(int i = a; i < n; i++)
#define repe(i,a,n) for(int i = a; i <= n; i++)
#define per(i,n,a) for(int i = n; i >= a; i--)
#define clc(a,b) memset(a,b,sizeof(a))
const int INF = 0x3f3f3f3f, MAXN = 510, MAXM = 5010;
struct NODE{
	int w,q,v;
	bool operator<(const NODE& t) const{
		return q+t.w < t.q+w;
	}
}p[MAXN];
int dp[MAXM];

int main()
{
#ifdef SHY
	freopen("e:\\1.txt", "r", stdin);
#endif
	int n,m;
	while(~scanf("%d %d%*c", &n, &m))
	{
		rep(i,0,n) scanf("%d %d %d%*c", &p[i].w, &p[i].q, &p[i].v);
		clc(dp,0);
		int ans = 0;
		sort(p,p+n);
		rep(i,0,n)
		{
			per(j,m,p[i].q)
			{
				dp[j] = max(dp[j], dp[j-p[i].w]+p[i].v);
			}
		}
		repe(i,1,m) ans = max(ans,dp[i]);
		printf("%d\n", ans);
	}
	return 0;
}


 


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值