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.

题意:01背包问题,就是用一定数量的钱可以购买到的最大的价值,但是在购买过程中会判断你拥有的钱是否小于某个值q,如果你的容量小于q,就无法购买,求可以买到的最大价值;
 
题解:
此题关键在于 发现购买商品会产生顺序的问题,

其实就是dp后效性的问题,除了i和v这2个维度还多了一个维度q,直接套用公式无法去除后效性,
先找到选择顺序的规律,去除后效性,再使用01背包;

选取的规律: 比如A:p1 q1, B:p2 q2,然后,假设单独买A或者B的话,都是可以买到的。
这时,若先买A,则你至少需要p1+q2的钱;若先买B,则至少需要p2+q1的钱。那肯定是花最少的钱咯,所以如果先买A再买B,那么p1+q2<p2+q1,
转换一下,就是q1-p1>q2-p2,也就是说qi-pi大的先买。
这里还得注意一点就是,排序的时候,得按照qi-pi从小到大排序,
因为你买第n件商品的时候,是在比较你是否要先买第n件商品。
打个比方让大家更好地理解,比如说f(3, 10),是不是max(f(2, 10-p3)+v3, f(2, 10)),
你会发现这个第一种情况f(2,10-p3)+v3中,是先买了第三件商品,也就是说排在后面的商品会先买。(重点)

还有一个要注意的:我们背包问题写的状态转移方程是从后往前选的(一维);

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; struct GOOD{
int c, q, w;
}g[]; bool cmp(GOOD x, GOOD y){
return x.q-x.c<y.q-y.c;
} int main()
{
int n, V, f[];
while(cin>>n>>V)
{
memset(f, , sizeof(f));
for(int i=; i<=n; i++)
cin>>g[i].c>>g[i].q>>g[i].w;
sort(g+, g+n+, cmp); for(int i=; i<=n; i++)
for(int v=V; v>=g[i].q; v--)
f[v]=max(f[v], f[v-g[i].c]+g[i].w);
cout<<f[V]<<endl;
}
return ;
}
 

HDU 3466 Proud Merchants(背包问题,要好好理解)的更多相关文章

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

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

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

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

  3. HDU 3466 Proud Merchants(01背包)

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

  4. hdu 3466 Proud Merchants 01背包变形

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

  5. HDU 3466 Proud Merchants(01背包)

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

  6. hdu 3466 Proud Merchants(有排序的01背包)

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

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

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

  8. HDU 3466 Proud Merchants(0-1背包)

    http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意: 最近,iSea去了一个古老的国家.在这么长的时间里,它是世界上最富有和最强大的王国.结果,这个国家 ...

  9. HDU 3466 Proud Merchants

    题目大意:现在给出商品,有三个参数,记为pi,qi,vi,vi是商品的在你心里价值,pi是商品的价格,qi是你要买商品的时候至少需要的钱然后求可得的最大价值. 单词积累:Merchants 商人  t ...

随机推荐

  1. JAVA获取汉字拼音首字母

    package com.common.util; import java.io.UnsupportedEncodingException; /** * 取得给定汉字串的首字母串,即声母串 * Titl ...

  2. 将pucharm与anaconda配合使用

    一个用来更新各种包,另一个负责美美的打代码,把pycharm, setting  project interpreter,选中anaconda中的python.exe.搞定.anaconda prom ...

  3. cocos creator 无法打开项目 dock栏只显示图标问题解决方法

    1.打开项目 2.找到 library和local文件夹 3.清空这两个文件夹的数据 4.问题已解决 原因: 以前生成的旧数据会和新数据发生冲突,把生成的旧数据清理掉就OK了.

  4. 惨痛第十届蓝桥杯总结(附录蓝桥省赛知识点总结)-C++ B组

    虽然目前距离蓝桥省赛仅仅过去一天但昨天下午和大神对答案的感觉依旧..... 现在深刻里理解到了为啥大神老是说咱们蓝桥叫 阅读理解杯(现在我非常认同这种说法啊...) 虽然第一次参加,赛前紧张提前30分 ...

  5. arcgis for JavaScript API 4.5与4.3的区别

    arcgis 4.5与4.3区别: 鉴于本人使用4.3时间比较久,而arcgis for JavaScript API于9月28日推出了4.5版本,但是直接更换4.5的init.js会出现意想不到的错 ...

  6. CentOS7 配置Mailx使用SMTP发送邮件

    mail.mailx和sendmail介绍: mail是mailx的别名,最初叫nail(与arch linux目前用的S-nail基因相同):mail是Heirloom的一个子项目.sendmail ...

  7. php 图片添加文字,水印

    因为工作需求,用到这个,网上找了很多,也没有找到好的方式,最后找到这种感觉比较简单的方式,记录下来,以备后用.   $im = imagecreatefrompng("img/yyk_bg. ...

  8. TypeError: '_io.TextIOWrapper' object does not support item assignment

    纯小白 遇到的细节问题: 报错 一开始看到这个傻逼了 TypeError: '_io.TextIOWrapper' object does not support item assignment 其实 ...

  9. liunx驱动----点亮LED

    自动挂接根文件系统(直接从NFS启动) 修改uboot命令行 把 bootargs=noinitrd root=/dev/mtdblock3 init=/linuxrc console=ttySAC0 ...

  10. Unicode,GBK和UTF8

    字符集 在介绍他们之间的区别时, 我们先讲下什么是Unicode. 简单来说,Unicode是一个字符集(character set), 和ASCII一样, 其作用是用一系列数字来表示字符(chara ...