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. Data Lake Analytics: 使用DataWorks来调度DLA任务

    DataWorks作为阿里云上广受欢迎的大数据开发调度服务,最近加入了对于Data Lake Analytics的支持,意味着所有Data Lake Analytics的客户可以获得任务开发.任务依赖 ...

  2. caffe 的docker安装过程及相关linux操作总结

    一.caffe 和 docker的安装编译 docker pull caffe镜像(注意使用docker安装省去安装CUDA和cudnn的安装.) 安装相关依赖包 安装opencv3(使用源码安装) ...

  3. listview显示固定条数

    看了很多网上其他大神的,感觉还是在listview的adapter中的getCount中下手比较好点 毕竟计算高度等等,那会让辅助的布局会一团糟,例如下面的搜索历史只显示四条,布局中有横向listvi ...

  4. webpack学习之—— 依赖图(Dependency Graph) 及 构建目标(Targets)

    Dependency Graph 任何时候,一个文件依赖于另一个文件,webpack 就把此视为文件之间有依赖关系.这使得 webpack 可以接收非代码资源(non-code asset)(例如图像 ...

  5. Android 对保存在 sharedpreference的重要数据进行编解码

    有时候为了登录方便会将用户名和密码保存在 sharedpreference里面,可是如果不加以处理密码将以明文保存. 在Android中java层提供了工具类:android.util.Base64; ...

  6. maven的配置和使用

    Maven 简介 1.1 Maven 是什么 翻译为“专家”,“内行” Maven是跨平台的项目管理工具.主要服务于基于Java平台的项目构建,依赖管理和项目信息管理. 1.2 为什么使用Maven ...

  7. Java练习 SDUT-2271_Eddy的难题

    Eddy的难题 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全 ...

  8. HDU 4193

    本题思路:用sum[]数组维护前缀和, 当然这里需要把原数组扩大为原来的两倍. 然后对于任意一个长度为n的区间 k.....k+n-1,如果有该区间内的最小值大于等于sum[k-1]那么该种情况就符合 ...

  9. GIL锁更加深刻理解

    参考链接:http://www.cnblogs.com/ajaxa/p/9111884.html

  10. iOS 后台定位

    http://www.cocoachina.com/ios/20150724/12735.html 前言 之前的文章说过 我现在做的是LBS定位的社交APP 其中主要的一个功能就是能够实时定位社交圈中 ...