HDOJ -- 3466 Proud Merchants

 Proud Merchants
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any more. 
The merchants were the most typical, each of them only sold exactly one item, the price was Pi, but they would refuse to make a trade with you if your money were less than Qi, and iSea evaluated every item a value Vi. 
If he had M units of money, what’s the maximum value iSea could get? 

Input

There are several test cases in the input. 
Each test case begin with two integers N, M (1 ≤ N ≤ 500, 1 ≤ M ≤ 5000), indicating the items’ number and the initial money. 
Then N lines follow, each line contains three numbers Pi, Qi and Vi (1 ≤ Pi ≤ Qi ≤ 100, 1 ≤ Vi ≤ 1000), their meaning is in the description. 
The input terminates by end of file marker. 

Output

For each test case, output one integer, indicating maximum value iSea could get. 

Sample Input

2 10
10 15 10
5 10 5
3 10
5 10 5
3 5 6
2 7 3

Sample Output

5
11

刚看完这道题一脸懵逼。。看了别人的解题思路,琢磨了半天,原来这道题也那么水。。仿照01背包2602的模板,做一点小小的改动就行啦。。
不过这道题还是很有价值的,最起码又学到了一个小技巧得意
题目大意:在一个古老的国家,那里奇葩的商人I只卖一种物品i,该物品的价格为Pi,但是当你手里的钱少于Qi时,商人I是不会把物品i卖给你的,而对你来说这间物品的价值是Vi,如果给你M钱,问你能买到 的物品 的最大价值是多少?
很明显要用01背包解题,但题目又多给出了一个限制条件Qi。。
其实只要找出每个商人所规定的物品差价(即买该物品时最少要有的钱Qi减去该物品的价格Pi),并将所有的商品按照这个差价升序就OK了。排好序就行了吗?的却如此!换个角度想:你手里的钱相当于背包的体积V,当我们做01背包问题时,就是让背包体积从0开始逐渐增加到V,每增加一次就对当前背包能容纳的所有的物品做一次筛选。同理:当把这些物品按照差价排好序后,这个差价就相当于物品的体积!!那么接下来就是01背包的舞台啦~~
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
	int p,q,v;
}a[505];
bool cmp(node A,node B){
	return A.q-A.p<B.q-B.p;
}//按照物品差价的升序排列 
int main(){
	int m,n;
	int dp[5010];
	while(~scanf("%d%d",&n,&m)){
		for(int i=0;i<n;i++)
			scanf("%d%d%d",&a[i].p,&a[i].q,&a[i].v);
		memset(dp,0,sizeof(dp));
		sort(a,a+n,cmp);
		for(int i=0;i<n;i++){
			for(int j=m;j>=a[i].q;j--)//确保手里剩余钱大于即将要买的物品的差价 
				dp[j]=max(dp[j],dp[j-a[i].p]+a[i].v);
			for(int k=1;k<=m;k++)
	            printf("%d ",dp[k]);
        	printf("\n");
		}
		printf("%d\n",dp[m]);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值