Proud Merchants

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

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
 
题意:给定 M 元钱,有 N 件商品,价格为 Wi ,价值为 Pi ,限制条件是手中钱少于 Qi 时不能购买该件商品。求可以获得的最大价值。
 
题解:
 

因为这个题目增加了购买的前提条件,和普通的01背包有点不同;

哪里不同呢?不同的地方在于普通的01背包,购买顺序不影响其结果;

但是在这里,我们可以很明白的看出来,购买顺序是会影响我们的最后结果的。

所以我们应该确定一个正确的购买顺序;然后我们就可以想想,如果叫你判断买不买,

实现价值最大化,你会以怎样的顺序先后判断;

很显然,你会先判断q大,p小的物品买不买,对吧,因为在你价值最大,你应该尽可能的判断q大,p小的物品;

明白这点,这个题目基本上就解决了;

我们可以对数据进行排序,直接就以p,q的差值排序就可以了;

但是这里我们需要注意的是,要以差值由小到大排序,而不是由大到小,

想想dp的过程,j=m,j--;后取得数在前面判断,这样才能让我们的数据更新不被影响;

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#define ll __int64
#define pi acos(-1.0)
#define mod 1
#define maxn 10000
using namespace std;
int n,m;
struct node
{
int p,q,v;
}N[];
int dp[];
bool cmp(struct node aa,struct node bb)
{
if((aa.q-aa.p)<(bb.q-bb.p))
return true;
return false;
}
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
memset(dp,,sizeof(dp));
memset(N,,sizeof(N));
for(int i=;i<=n;i++)
scanf("%d %d %d",&N[i].p,&N[i].q,&N[i].v);
sort(N+,N++n,cmp);
for(int i=;i<=n;i++)
{
for(int l=m;l>=N[i].q;l--)
dp[l]=max(dp[l],dp[l-N[i].p]+N[i].v);
}
cout<<dp[m]<<endl;
}
return ;
}

HDU 3446 有贪心思想的01背包的更多相关文章

  1. HDU 5501:The Highest Mark 01背包

    The Highest Mark  Accepts: 71  Submissions: 197  Time Limit: 2000/1000 MS (Java/Others)  Memory Limi ...

  2. HDU 3639 Bone Collector II(01背包第K优解)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. HDU 2126 Buy the souvenirs (01背包,输出方案数)

    题意:给出t组数据 每组数据给出n和m,n代表商品个数,m代表你所拥有的钱,然后给出n个商品的价值 问你所能买到的最大件数,和对应的方案数.思路: 如果将物品的价格看做容量,将它的件数1看做价值的话, ...

  4. HDU 1203 I NEED A OFFER! 01背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1203 解题思路:简单的01背包,用dp[i]表示花费不超过i时的最大可能性 状态转移方程 dp[i]= ...

  5. HDU 2639 Bone Collector II【01背包 + 第K大价值】

    The title of this problem is familiar,isn't it?yeah,if you had took part in the "Rookie Cup&quo ...

  6. hdu 2639 Bone Collector II (01背包,求第k优解)

    这题和典型的01背包求最优解不同,是要求第k优解,所以,最直观的想法就是在01背包的基础上再增加一维表示第k大时的价值.具体思路见下面的参考链接,说的很详细 参考连接:http://laiba2004 ...

  7. HDU 3496 (二维费用的01背包) Watch The Movie

    多多想看N个动画片,她对这些动画片有不同喜欢程度,而且播放时长也不同 她的舅舅只能给她买其中M个(不多不少恰好M个),问在限定时间内观看动画片,她能得到的最大价值是多少 如果她不能在限定时间内看完买回 ...

  8. hdu 1203 I NEED A OFFER (0-1背包)

    题意分析:0-1背包变形  递推公式:dp[i] = max(dp[i], 1-(1-dp[i-C])*(1-p)) /* I NEED A OFFER! Time Limit: 2000/1000 ...

  9. HDU 3339 In Action 最短路+01背包

    题目链接: 题目 In Action Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. QP之QEP原理

    1.QP简介: 量子平台(Quantum Platform, 简称QP)是一个用于实时嵌入式系统的软件框架,QP是轻量级的.开源的.基于层次式状态机的.事件驱动的平台. QP包括事件处理器(QEP). ...

  2. linux实现DNS轮询实现负载平衡

    DNS 轮询机制会受到多方面的影响,如:A记录的TTL时间长短的影响:别的 DNS 服务器 Cache 的影响:windows 客户端也有一个DNS Cache.这些都会影响 DNS 轮询的效果.因此 ...

  3. PHP中的面向对象魔术方法大全

    1.__construct  构造方法 2.__destruct  析构方法 3.__get 获取成员值 4.__set 设定成员值 5.__isset 判断成员值 6.__unset unset成员 ...

  4. 基于vue来开发一个仿饿了么的外卖商城(一)

    一.准备工作 1.大前提:已安装好node. npm. vue. vue-cli.stylus(此项目使用stylus来编译) 2.开发软件:Google Chrome(建议安装插件vue-devto ...

  5. 今天买了个pro,开始ios开发

    今天买了个mac pro 开始ios开发啦,爽!

  6. python 网络编程(远程执行命令与粘包)

    远程执行命令 先来学习一个新模块 , 一会用到的.. 新模块: subprocess 执行系统命令 r = subprocess.Popen('ls',shell=True,stdout=subpro ...

  7. (转)基于CUDA的GPU光线追踪

    作者:Asixa 链接:https://zhuanlan.zhihu.com/p/55855479 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.     替STL. ...

  8. centos7安装python3.7

    Centos7安装Python3的方法   由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. [root@VM_105_217_ ...

  9. SetWindowPos,RegisterHotKey,GlobalAddAtom的用法

    还以为SetWindowPos是给Frm的子框架间编写的,原来是给mainfrm写的,可以把你写的主窗口置顶,置底(看样子应该可以变成桌面了,还没试呢,才忙到现在...) 子窗口的遮挡可以使用窗口的样 ...

  10. 九度OJ--Q1167

    import java.util.Scanner;import java.util.TreeSet; /* * 题目描述: * 输入一个数组的值,求出各个值从小到大排序后的次序. * 输入: * 输入 ...