Proud Merchants

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 4039    Accepted Submission(s):
1677

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
 
Author
iSea @ WHU
 
Source
Recommend
zhouzeyong   |   We have carefully selected several
similar problems for you:  3460 3463 3468 3467 3465 
 
简单的01背包,排个序就好,代码很简单。
复制一下网上大神的思路:

因为如果一个物品是5 9,一个物品是5 6,对第一个进行背包的时候只有dp[9],dp[10],…,dp[m],再对第二个进行背包的时候,如果是普通的,应该会借用前面的dp[8],dp[7]之类的,但是现在这些值都是0,所以会导致结果出错。于是要想到只有后面要用的值前面都可以得到,那么才不会出错。设A:p1,q1 B:p2,q2,如果先A后B,则至少需要p1+q2的容量,如果先B后A,至少需要p2+q1的容量,那么就是p1+q2 > p2+q1,变形之后就是q1-p1 < q2-p2。

所以要针对每个属性的q-p来进行排序

题意:每个物品有p、q、v,三个属性,每个物品的话费为p,但是前提是必须有q,v则是得到的价值。

附上代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int p,q,v;
} ss[]; int maxs(int a,int b)
{
return a>b?a:b;
}
bool cmp(node a,node b)
{
return a.q-a.p<b.q-b.p; //关键排序,详见上文分析
}
int main()
{
int n,m,i,j;
int dp[];
while(~scanf("%d%d",&n,&m))
{
for(i=; i<n; i++)
scanf("%d%d%d",&ss[i].p,&ss[i].q,&ss[i].v);
sort(ss,ss+n,cmp);
memset(dp,,sizeof(dp));
for(i=; i<n; i++)
for(j=m; j>=ss[i].q; j--)
dp[j]=maxs(dp[j],dp[j-ss[i].p]+ss[i].v);
printf("%d\n",dp[m]);
}
return ;
}

hdu 3466 Proud Merchants(有排序的01背包)的更多相关文章

  1. HDU 3466 Proud Merchants 带有限制的01背包问题

    HDU 3466 Proud Merchants 带有限制的01背包问题 题意 最近,伊萨去了一个古老的国家.在这么长的时间里,它是世界上最富有.最强大的王国.因此,即使他们的国家不再那么富有,这个国 ...

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

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

  3. hdu 3466 Proud Merchants 01背包变形

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

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

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

  5. HDU 3466 Proud Merchants(01背包)

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

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

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

  7. hdu 3466 Proud Merchants 自豪的商人(01背包,微变形)

    题意: 要买一些东西,每件东西有价格和价值,但是买得到的前提是身上的钱要比该东西价格多出一定的量,否则不卖.给出身上的钱和所有东西的3个属性,求最大总价值. 思路: 1)WA思路:与01背包差不多,d ...

  8. HDU 3466 Proud Merchants(01背包)

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

  9. HDU 3466 Proud Merchants(背包问题,要好好理解)

    Problem Description Recently, iSea went to an ancient country. For such a long time, it was the most ...

随机推荐

  1. stack的基本使用方式

    #include <iostream> #include <stack>//头文件 using namespace std; stack<int> S; int m ...

  2. Vue--moment时间格式插件安装和使用

    使用链接:http://momentjs.cn/ 1.安装monent 2.导入 3.过滤器   4.template模板使用:

  3. Hdu 4251 区间中位数(划分树)

    题目链接 The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  4. vue-cli3 关闭eslint

    关闭eslint 直接注释掉package.json文件中eslint的配置就可以了(以下是vue-cli的默认配置) "eslintConfig": { "root&q ...

  5. 在Liferay 7中如何自定义一个Portlet的toolbar

    哈哈,懒得自己写了,直接贴教程了,你想为那个portlet添加自定义的toolbar,就在javax.portlet.name=属性中写上它的值.教程博客:Adding Portlet URL in ...

  6. Docker数据管理-数据卷 data volumes和数据卷容器data volumes containers的使用详解

    此文来源于:https://yq.aliyun.com/ziliao/43471 参考原文件之外,做了些修改. Volume数据卷是Docker的一个重要概念.数据卷是可供一个或多个容器使用的特殊目录 ...

  7. Neo4j学习笔记(1)——使用Java API实现简单的增删改查

    阅读目录 项目的创建及配置 使用嵌入式数据库 创建节点和关系 查询及更新 删除关系和节点 完整代码 参考资料 回到顶部 项目的创建及配置 因为Neo4j依赖的jar包比较多,所以推荐使用Maven来管 ...

  8. 跟我一起认识axure(二)

    创建企业网站页面步骤 第一步修改这里 变成 第一部分就完成了 第二部分部件窗口 在Axure中设计页面像小时候玩的拼图游戏,那么部件窗口就是专门用来存放拼图块的容器 使用部件窗口中常用的部件设计欢迎页 ...

  9. scrapy 调用js

    依赖: PyExecJS 使用案例: from execjs import execjs text = response.xpath('//script/text()')[1].get() ctx = ...

  10. 【JZOJ4859】【NOIP2016提高A组集训第7场11.4】连锁店

    题目描述 Dpstr开了个饮料连锁店,连锁店共有n家,出售的饮料种类相同.为了促销,Dpstr决定让每家连锁店开展赠送活动.具体来说,在第i家店,顾客可以用ai个饮料瓶兑换到bi瓶饮料和1个纪念币(注 ...