题目传送门 

Proud Merchants

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

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

  分析:一开始考虑直接01背包,将限制条件直接换成j>=q[i],但是发现这种思路是错的。显然物品的顺序会对其产生影响,那么就要考虑如何排序。然后试了几种排序发现都不对,还是K_lord和five20大佬讲过以后才明白。
  设两个物品a,b,那么如果在选了一个物品以后,要让剩余的钱能买到的物品尽可能的多,则要满足qa+pb<qb+pa。或者这么说,假如你有m的金钱,而且m>qb>qa,m-pa>qb,m-pb>qa,那么如果先选b就不能选a,但如果先选a就可以选b,那么上面的式子成立。再变换一下得到qa-pa<qb-pb,那么就按照这个式子排序然后01背包就OK了。
  Code:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<iomanip>
using namespace std;
const int N=5e3+;
int n,m,dp[N];
struct Node{int p,q,v;}a[N];
bool cmp(Node x,Node y)
{return x.q-x.p<y.q-y.p;}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>m){
for(int i=;i<=n;i++)
cin>>a[i].p>>a[i].q>>a[i].v;
memset(dp,,sizeof(dp));
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++)
for(int j=m;j>=a[i].q;j--)
dp[j]=max(dp[j],dp[j-a[i].p]+a[i].v);
cout<<dp[m]<<"\n";}
return ;
}

HDU3466 Proud Merchants [背包]的更多相关文章

  1. HDU3466 Proud Merchants[背包DP 条件限制]

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

  2. hdu3466 Proud Merchants(01背包)

    https://vjudge.net/problem/HDU-3466 一开始想到了是个排序后的背包,但是排序的策略一直没对. 两个物品1和2,当p1+q2>p2+q1 => q1-p1& ...

  3. HDU--3466 Proud Merchants (01背包)

    题目http://acm.hdu.edu.cn/showproblem.php?pid=3466 分析:这个题目增加了变量q 因此就不能简单是使用01背包了. 网上看到一个证明: 因为如果一个物品是5 ...

  4. [hdu3466]Proud Merchants

    题目描述 Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and po ...

  5. Proud Merchants(01背包变形)hdu3466

    I - Proud Merchants Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  6. Proud Merchants(POJ 3466 01背包+排序)

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

  7. Proud Merchants(01背包)

    Proud Merchants Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) To ...

  8. HDU 3466 Proud Merchants(01背包)

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

  9. hdu 3466 Proud Merchants 01背包变形

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

随机推荐

  1. sql文件导入时出错

    使用Navicat 连接工具连接mysql数据库. mysql数据库建立后,导入sql文件报错: [Err] 1064 - You have an error in your SQL syntax; ...

  2. 阿里云maven仓库地址,速度提升100倍

    参照:https://www.cnblogs.com/xxt19970908/p/6685777.html maven仓库用过的人都知道,国内有多么的悲催.还好有比较好用的镜像可以使用,尽快记录下来. ...

  3. NGINX: 反向代理 Nexus

    Nginx 反向代理 nexus 的服务, 一直卡在 Initialize... 解决方式是添加一个 header X-Forwarded-Proto: proxy_set_header X-Forw ...

  4. Android中自定义属性attr.xml的格式详解

    1. reference:参考某一资源ID.     (1)属性定义:             <declare-styleable name = "名称">      ...

  5. Select 使用不当引发的core,你应该知道的

    排查一个死机问题,搞了好几天时间,最终确定原因:最终确定问题原因,在此分享一下: 第一步:常规根据core文件查看栈信息,gdb –c core xxxx 如下rip不正确,指令地址错乱,栈信息已破坏 ...

  6. Java多线程学习(二)synchronized关键字(2)

    转载请备注地址:https://blog.csdn.net/qq_34337272/article/details/79670775 系列文章传送门: Java多线程学习(一)Java多线程入门 Ja ...

  7. 去掉每行的特定字符py脚本

    百度下载一个脚本的时候遇到那么一个情况.每行的开头多了一个空格.https://www.0dayhack.com/post-104.html 一个个删就不要说了,很烦.于是就有了下面这个脚本. #! ...

  8. 从零开始PHP攻略(000)——关于WAMPServer集成环境

    Apache.PHP和MySQL都可以用于多种操作系统和Web服务器的组合.本篇介绍在Windows下用WampServer环境包来搭建本地php环境. W:windows A:Apache M:My ...

  9. 通用套接字选项和TCP套接字选项

    1. 套接字选项函数原型: #include <sys/socket.h> int getsockopt(int sockfd, int level, int optname, void ...

  10. python基础===zmail,收发邮件的模块

    项目地址: GitHub:https://github.com/ZYunH/zmail  介绍: https://mp.weixin.qq.com/s?__biz=MzAxMjUyNDQ5OA==&a ...