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. bzoj1823满汉全席

    2-sat模板 这篇博客写得非常好 传送门 //Achen #include<algorithm> #include<iostream> #include<cstring ...

  2. Codeforces 113B

    题目链接 B. Petr# time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. 【JZOJ5060】【GDOI2017第二轮模拟day1】公路建设 线段树+最小生成树

    题面 在Byteland一共有n 个城市,编号依次为1 到n,它们之间计划修建m条双向道路,其中修建第i 条道路的费用为ci. Byteasar作为Byteland 公路建设项目的总工程师,他决定选定 ...

  4. Python学习笔记(三)字符串类型及其操作(2)

    1.字符串的表示 字符串是字符的序列表示,可以由一对单引号(‘).双引号(“)或三引号(’‘’)构成.其中,单引号.双引号和三引号都可以表示单行字符串,但是只有三引号可以表示多行字符串 在使用双引号时 ...

  5. web服务器与tomcat

    web服务器与tomcat 服务器分类: 硬件服务器和软件服务器 web服务器: 提供资源供别人访问 web: 网页的意思,资源. web资源分类: 动态的web资源:内容有可能发生改变的 静态的we ...

  6. 关于android SDK安装Failed to fetch URL http://dl-ssl.google.com/android/repository/addons_list-1.xml出错

          最近SDK出问题了,然后在google下载了一个android-sdk-windows.rar,然后点击SDK Manager,结果一直不能刷新API Level,然后就开始在网上找了好多 ...

  7. iOS:你App的设置做对了吗?

    http://www.cocoachina.com/ios/20151217/14707.html iOS 8及以上版本最不为人知的一个特点是与应用设置的深层链接,用户可以根据APP的需要授权启用位置 ...

  8. python 利用csv模块导入数据

  9. css面试题总结(转)

    转自此网页http://www.cnblogs.com/YangqinCao/p/5721810.html. 1.两栏布局,左边栏宽度固定,适应父元素高度变化 首先分析两栏布局, 两栏布局两种常见方法 ...

  10. N!分解素因子及若干问题【转载】

    这里写的非常好http://www.cnblogs.com/openorz/archive/2011/11/14/2248992.html,感谢博主,我这里就直接用了. 将N!表示成 N! = p1^ ...