Proud Merchants

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 5
Problem 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背包。初看之下似乎和普通背包差不多,判容量大于q时才装。但是这会出大问题,如果一个物品p = 5,q = 7,一个物品p = 5,q = 9,如果先算第一个,那么当次只有7,8...m可以进行状态转移,装第二个物品的时候9,10..m进行转移,第二个物品转移就可以借用第一个物品的那些个状态,而第二个物品先转移,第一个再转移则不能。当然,还有价格有关,当限制一样价格不同时顺序就影响结果。一种组合的排序策略--限制又小价格又贵的先选,也就是q-p小的先选。为什么这样呢?A:p1,q1 B: p2,q2,先选A,则至少需要p1+q2的容量,而先选B则至少需要p2+q1,如果p1+q2>p2+q1,那么要选两个的话的就要先选A再选B,公式可换成q1-p1 > q2-p2,就按这样的方法排序最后的顺序就是最优的顺序。

该题要确保P[i]-Q[i]小的先被”挑选“,差值越小使用它的价值越大(做出的牺牲越小).

代码:

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX(x,y)(x>y?x:y)
const int MAXN=;
const int MAXM=;
struct Node{
int p,q,v;
};
int cmp(const void *a,const void *b){
if((*(Node *)a).q-(*(Node *)a).p<(*(Node *)b).q-(*(Node *)b).p)return -;
else return ;
}
Node dt[MAXM];
int bag[MAXN];
int main(){
int N,M;
while(~scanf("%d%d",&N,&M)){
memset(bag,,sizeof(bag));
for(int i=;i<N;i++)scanf("%d%d%d",&dt[i].p,&dt[i].q,&dt[i].v);
qsort(dt,N,sizeof(dt[]),cmp);
for(int i=;i<N;i++)
for(int j=M;j>=dt[i].q;j--){//
if(j>=dt[i].p){//
bag[j]=MAX(bag[j],bag[j-dt[i].p]+dt[i].v);
}
}
printf("%d\n",bag[M]);
}
return ;
}

Proud Merchants(01背包)的更多相关文章

  1. hdu 3466 Proud Merchants 01背包变形

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  2. HDU 3466 Proud Merchants(01背包)

    题目链接: 传送门 Proud Merchants Time Limit: 1000MS     Memory Limit: 65536K Description Recently, iSea wen ...

  3. HDU 3466 Proud Merchants(01背包问题)

    题目链接: 传送门 Proud Merchants Time Limit: 1000MS     Memory Limit: 65536K Description Recently, iSea wen ...

  4. HDU 3466 Proud Merchants 排序 背包

    题意:物品有三个属性,价格p,解锁钱数下线q(手中余额>=q才有机会购买该商品),价值v.钱数为m,问购买到物品价值和最大. 思路:首先是个01背包问题,但购买物品受限所以应先排序.考虑相邻两个 ...

  5. Proud Merchants HDU - 3466 (思路题--有排序的01背包)

    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...

  6. HDU 3466 Proud Merchants【贪心 + 01背包】

    Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerfu ...

  7. hdu 3466 Proud Merchants(有排序的01背包)

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  8. Proud Merchants(POJ 3466 01背包+排序)

    Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  9. HDU 3466 Proud Merchants(01背包)

    这道题目看出背包非常easy.主要是处理背包的时候须要依照q-p排序然后进行背包. 这样保证了尽量多的利用空间. Proud Merchants Time Limit: 2000/1000 MS (J ...

随机推荐

  1. 如何实现Linux下的U盘(USB Mass Storage)驱动

    如何实现Linux下的U盘(USB Mass Storage)驱动 版本:v0.7 How to Write Linux USB MSC (Mass Storage Class) Driver Cri ...

  2. 跟我开发NSP(网上查询平台):如何选择开发项目

    我想通过一个真实的项目开发的全过程,记录一下开发过程的点点滴滴,记录一下过程中的前思后想.这个全过程包括,如何选择项目.如何分析项目.如何组织项目开发.如何设计开发流程.如何设计软件的总体架构.如何建 ...

  3. 三校联考 Day3

    三校联考 Day3 大水题 题目描述:给出一个圆及圆上的若干个点,问两个点间的最远距离. solution 按极角排序,按顺序枚举,显然距离最远的点是单调的,线性时间可解出答案. 大包子的束缚 题目描 ...

  4. VC++6.0下通过opencv读入图像并反色

    第一个opencv测试程序: 不多说,直接上代码,代码注释很详尽: ////////////////////////////////////////////////////////////////// ...

  5. uva10815 by sixleaves

    题目很简单.其实stringstream就的用法和iosteam差不多,所以学习起来是很简单的.stringstream类里面有一个string缓存,str()和str(string)成员函数.前者用 ...

  6. Kafka的可靠性问题

    a. Producer到broker 把request.required.acks设为1,丢会重发,丢的概率很小 b. Broker b.1 对于broker,落盘的数据,除非磁盘坏了,不会丢的 b. ...

  7. CSS备忘-1

    CSS 可以通过以下方式添加到HTML中: 内联样式- 在HTML元素中使用"style" 属性 内部样式表 -在HTML文档头部 <head> 区域使用<sty ...

  8. 随机抽样一致性算法(RANSAC)

    本文翻译自维基百科,英文原文地址是:http://en.wikipedia.org/wiki/ransac,如果您英语不错,建议您直接查看原文. RANSAC是"RANdom SAmple ...

  9. 成本卷积报错:CSTPSCEX.explode_sc_cost_flags():40:ORA-01476: 除数为 0

    成本卷积请求:供应链成本累计 - 打印报表 运行后报一下错误: MSG-00000: Rollup ID = 236403MSG-00000: Before CSTPSCEX.supply_chain ...

  10. sql从某不连续的数字中将其分段并找出缺失的数字并分段

    首先做准备数据 )) ') ') ') ') ') ') ') ') ') ') ') ') ') ') ') ') 将数据转换成应该处理的数据格式 ),colValue INT ) ) ,LEN(c ...