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. Keras RetinaNet github项目安装

    在存储库目录/keras-retinanet/中,执行pip install . --user 后,出现错误: D:\>cd D:\JupyterWorkSpace\keras-retinane ...

  2. 实现hibernate 的validator校验

    Validator校验分为快速校验和全校验.快速校验是当遇到第一个参数不符合条件时,立即停止校验程序,将校验不通过的信息返回到前端:全校验是将前端传过来的参数全部进行校验,将所有不通过校验的信息一起返 ...

  3. mac电脑安装wxPython2.8.12.1不成功怎么办 , Could not find a version that satisfies the requirement 2.8.12.1

    目的:robotframe-ride用于接口测试 遇到的问题: 1.mac终端pip安装robotframework-ride后 pip install robotframework-ride (pi ...

  4. sort();对结构体数组的排序

    sort(); 位于C++ 头文件 #include<algorithm>中 数组排序(从小到大,从大到小) 结构体排序(数字参数从大到小...字符串为参数 字典序....) 代码示例:( ...

  5. windows----------如何禁用PC端微信的开机启动

    1.开始菜单--->运行--->输入msconfig 2.如下图,然后点击启动 3.打开任务管理器 4.右键wechat,然后禁用.

  6. 利用反射和JDBC元数据实现更加通用的查询方法

    package com.at221.jdbc; import java.io.IOException; import java.io.InputStream; import java.sql.*; i ...

  7. 线性二次型调节器LQR/LQC算法解析及求解器代码(matlab)

    参考链接:http://120.52.51.14/stanford.edu/class/ee363/lectures/dlqr.pdf 本文参考讲义中的第20页PPT,根据Hamilton-Jacob ...

  8. 在dotnetcore的MVC项目中,创建支持 vue.js 的最小工程模板

    网上Vue模板不是最新的,我自己做了一个最新的支持 Vue.js 的最小工程模板,方便大家从 Hello world. 入门, 在 VS2017 里学习,并扩展出自己的项目. 下面是创建步骤: 1.在 ...

  9. 2018-2019-2 20165316 《网络对抗技术》Exp1 PC平台逆向破解

    2018-2019-2 20165316 <网络对抗技术>Exp1 PC平台逆向破解 1 逆向及Bof基础实践说明 1.1 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件 ...

  10. restful 跨域

    同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能. 就浏览器而言的, http://127.0.0.1:8000  协议 域名 端口 跨域 问题// 简单 ...