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

题意: 有 n 个物品,每个物品都有一定的价值和花费,而且买的时候自己的钱不能低于那个物品的指标,问最后可以买到的物品的最大价值是多少。

思路: 需要对物品按 q-p 的值从小到大排序,因为这样可以保证每次更新的状态值从小到大递增,前面更新过的状态不会影响后面更新的状态。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0x3fffffff;
const long long MOD = ;
typedef long long LL;
#define met(a,b) (memset(a,b,sizeof(a))) struct node
{
int p, q, v;
bool friend operator < (node n1, node n2)
{
return (n1.q-n1.p)<(n2.q-n2.p);
}
}a[N]; int dp[N]; int main()
{
int n, m; while(scanf("%d%d", &n, &m)!=EOF)
{
int i, j; met(a, );
met(dp, ); for(i=; i<=n; i++)
scanf("%d%d%d", &a[i].p, &a[i].q, &a[i].v); sort(a, a+n); for(i=; i<=n; i++)
{
for(j=m; j>=a[i].q; j--)
{
dp[j] = max(dp[j], dp[j-a[i].p]+a[i].v);
}
} printf("%d\n", dp[m]);
}
return ;
}

(01背包 先排序)Proud Merchants (hdu 3466)的更多相关文章

  1. Proud Merchants HDU - 3466 (思路题--有排序的01背包)

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

  2. Proud Merchants HDU - 3466 01背包&&贪心

    最近,我去了一个古老的国家.在很长一段时间里,它是世界上最富有.最强大的王国.结果,这个国家的人民仍然非常自豪,即使他们的国家不再那么富有.商人是最典型的,他们每个人只卖一件商品,价格是Pi,但是如果 ...

  3. Re0:DP学习之路 Proud Merchants HDU - 3466

    解法 排序+01背包 这里的排序规则用q-p升序排列这里是一个感觉是一个贪心的策略,为什么这样做目前也无法有效的证明或者说出来 然后就是01背包加了一个体积必须大于什么值可以装那么加一个max(p,q ...

  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: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

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

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

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

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

  8. HDU 3466 Proud Merchants(01背包)

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

  9. hdu 3466 Proud Merchants 自豪的商人(01背包,微变形)

    题意: 要买一些东西,每件东西有价格和价值,但是买得到的前提是身上的钱要比该东西价格多出一定的量,否则不卖.给出身上的钱和所有东西的3个属性,求最大总价值. 思路: 1)WA思路:与01背包差不多,d ...

随机推荐

  1. js 正则表达式:密码必须由6-12位数字加字母组成

    ^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$

  2. 小程序41028 form_id无效

    如果参数都没有问题的话,那么我的问题来了,你是发给用户自己么?如果不是,那就找到原因了,必须发给本人才可以...我淌过无数条坑,这个坑我服了...官方文档上写的不是很清楚

  3. python 大量使用json 存储数据时,格式化输出的方式

    import json, pprint dic = {'name': 234, 'user_name': 'yan xia ting yu ', 'list': ['ds', 'a', 2], '你好 ...

  4. .net如何处理高并发socket,建立高性能健壮的socket服务

    看到一个问题,说如何保持5000-10000+的健壮的socket服务. 初学者肯定是会把每个连接都开一个线程来监听.这很浪费资源 通常只会(动态地)占用几个线程就能保持3000个长连接会话了.“为每 ...

  5. nginx日志

    相关知识可参考文章:nginx日志格式及自定义日志配置 1.查看nginx的log配置 1)vim /etc/nginx/nginx.conf 打开为 user nginx;worker_proces ...

  6. PAT 1027 打印沙漏(20)(思路)

    1027 打印沙漏(20)(20 分) 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个"*",要求按下列格式打印 ***** *** * *** ***** 所谓& ...

  7. 开源项目CircleImageView

    1.在自己MainActivity所在包下创建CircleImageView.class文件 package 自己包名; import android.content.Context; import ...

  8. svn冲突问题解决办法

    经常有人会说,树冲突是很难解决的一类冲突,其实一旦了解了其原理,要解决也不难.先回顾下对于树冲突的定义.     树冲突:当一名开发人员移动.重命名.删除一个文件或文件夹,而另一名开发人员也对它们进行 ...

  9. Linux 终端设备

    <Linux终端设备详解> https://www.cnblogs.com/shineshqw/articles/2423989.html

  10. Django报错:__init__() missing 1 required positional argument: 'on_delete'

    原因: 在django2.0后,定义外键和一对一关系的时候需要加on_delete选项,此参数为了避免两个表里的数据不一致问题,不然会报错:TypeError: __init__() missing ...