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. django的HttpResponse对象

    服务器接收到http协议的请求后,会根据报文创建HttpRequest对象,这个对象不需要我们创建,直接使用服务器构造好的对象就可以.视图的第一个参数必须是HttpRequest对象,在django. ...

  2. Kubernetes-深入分析集群安全机制

    Kubernetes过一系列机制来实现集群的安全机制,包括API Server的认证授权.准入控制机制及保护敏感信息的Secret机制等.集群的安全性必须考虑以下的几个目标: 保证容器与其所在宿主机的 ...

  3. 杭电 1003 Max Sum (动态规划)

    参考:https://www.cnblogs.com/yexiaozi/p/5749338.html #include <iostream> #include <cstdio> ...

  4. C语言运算符优先级和结合性

    运算符优先级和结合性 优先级                                       运算符 结合性                                         ...

  5. Java——static关键字---18.09.27

    static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但在Java语言中没有全局变量的概念. static关键字主要有两种作用: 一.为某特定数据类 ...

  6. fiddler手机抓包配置方法

    一.下载工具包 百度搜索”fiddler 下载“ ,安装最新版本 下载的软件安装包为“fiddler_4.6.20171.26113_setup.exe”格式,双击安装.安装成功,在“开始”-“所有程 ...

  7. Xshell入门教程介绍

    免费软件 Xshell和 Xftp 都是 NetSarang 出品的优秀网络管理.安全传输工具.Xshell 是一个免费的安全终端仿真器,可以作为 SSH.TELNET 或 RLOGIN 的终端模拟, ...

  8. php-configure错误解决

    configure: error: libjpeg.(a|so) not foundconfigure: error: libjpeg.(a|so) not foundln -sf libjpeg.s ...

  9. Windows Server Backup 裸机恢复

    1.打开“Windows Server Backup”选择本地备份,并在操作栏选择“一次性备份”:(在实际生产环境中可以根据自己的需求,选择一次性备份还是选择备份计划.) 2.打开“一次性备份向导”, ...

  10. 代码混淆 iOS

    该方法只能针对有.m.h的类进行混淆,静态库等只有.h文件的没法进行混淆 代码混淆,刚刚看到是不是有点懵逼,反正我是最近才接触到这么个东西,因为之前对于代码和APP,只需要实现功能就好了,根本没有考虑 ...