Proud Merchants

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

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
 
Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:  3460 3463 3468 3467 3465 
 
贪心 + 01背包
只是这个贪心的思想我领悟了好久的说,我自个做了好久,却发现不管我按那个排都不合适啊,这是为何啊?
后来我就看了题解,难怪我弄得不对呢,排序竟然是按p - q排的,简单证明如下:
假设有A、B两个物品,A的花费为p1, 限制金额为q1,B的花费为p2,,限制金额为q2。那么有两种情况:
1、先A后B所需空间:p1 + q2
2、先B后A所需空间:p2 + q1
对本题而言,若想在进行一个物品的购买时他所需的前面的状态都已更新过,那么需要所需空间大的在前面,即 p1 + q2 > p2 + q1则A在前面,整理得 :
                                                               p1 - q1 > p2 - q2
eg:对于p1 = 5, q1 = 10; p2 = 3, q2 = 5;
            V1 = p1 + q2 = 10;
            V2 = p2 + q1 = 13;
            在进行d[10] = max(d[10], d[10 - 5] + v(第一个物品的价值))即d[5]应该已经更新过,则应先2后1。
接下来就是简单的01背包了。
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
#define ll long long
#define _cle(m, a) memset(m, a, sizeof(m))
#define repu(i, a, b) for(int i = a; i < b; i++)
#define repd(i, a, b) for(int i = b; i >= a; i--)
#define sfi(n) scanf("%d", &n)
#define sfl(n) scanf("%I64d", &n)
#define pfi(n) printf("%d\n", n)
#define pfl(n) printf("%I64d\n", n)
#define MAXN 5005 int dp[MAXN];
struct P{
int p, q, v;
bool operator < (const P& t) const {
return p - q > t.p - t.q;
}
}b[MAXN];
int main()
{
int n, m;
while(~scanf("%d%d", &n, &m))
{
repu(i, , n) sfi(b[i].p), sfi(b[i].q), sfi(b[i].v);
_cle(dp, );
sort(b, b + n);
repu(i, , n)
for(int j = m; j >= b[i].q && j >= b[i].p; j--)
dp[j] = max(dp[j], dp[j - b[i].p] + b[i].v);
pfi(dp[m]);
}
return ;
}

HDU 3466的更多相关文章

  1. HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)

    HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...

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

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

  3. (01背包 先排序)Proud Merchants (hdu 3466)

    http://acm.hdu.edu.cn/showproblem.php?pid=3466   Description Recently, iSea went to an ancient count ...

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

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

  5. HDU 3466(01背包变种

    http://acm.hdu.edu.cn/showproblem.php?pid=3466 http://www.cnblogs.com/andre0506/archive/2012/09/20/2 ...

  6. hdu 3466 01背包变形【背包dp】

    http://acm.hdu.edu.cn/showproblem.php?pid=3466 有两个物品P,Q,V分别为 3 5 6, 5 10 5,如果先dp第一个再dp第二个,背包容量至少要为3+ ...

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

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

  8. hdu 3466 排序01背包

    也是好题,带限制的01背包,先排序,再背包 这题因为涉及到q,所以不能直接就01背包了.因为如果一个物品是5 9,一个物品是5 6,对第一个进行背包的时候只有dp[9],dp[10],…,dp[m], ...

  9. HDU 3466 Proud Merchants(01背包)

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

随机推荐

  1. onclick事件分析

     有些时候,我们想实现这样的一种效果:      <a href="imgs/2.jpg" title="A fireworks display" onc ...

  2. new的深一步

    new的深一步 new运算符 用于创建对象和条用构造函数 new修饰符 用于隐藏基类中被继承的成员 new约束 用于在泛型声明中约束可能用作类型参数的参数类型 new运算符 用于创建对象和调用构造函数 ...

  3. 网页自适应@media

    @media (min-width: 768px){ }/*屏幕最小为768px时调用括号里的属性*/ @media (max-width: 767px) {} /*屏幕最大为768px时调用括号里的 ...

  4. JAVA中对Cookie的操作

    (1)往 Cookie 中存值: <%@page import="javax.xml.ws.Response"%> <%@ page language=" ...

  5. php生成mysql的数据字典

    <?php header('content-type:text/html;charset=utf-8'); define('DB_HOST','localhost'); define('DB_U ...

  6. 用JS打开网页时自动更改css样式,可用于处理浏览器兼容

    代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...

  7. php按条件查询的数据分页显示,点击下一页时又列出全部数据的解决办法

    其实很简单,只要把表单提交方式改为get方式就行了,然后调用分页函数: function getpage(&$m,$where,$pagesize=10){ $m1=clone $m;//浅复 ...

  8. iOS开发之 XCode6.0的iOS免证书真机测试方法(MAC及黑苹果均有效)

    参考:http://mobile.51cto.com/iphone-455500.htm XCode6.0的iOS免证书真机测试方法(MAC及黑苹果均有效) 前提:设备已经越狱 目前在XCode上开发 ...

  9. android瀑布流效果(仿蘑菇街)

    Android 转载分享(10)  我们还是来看一款示例:(蘑菇街)           看起来很像我们的gridview吧,不过又不像,因为item大小不固定的,看起来是不是别有一番风味,确实如此. ...

  10. 转:C的|、||、&、&&、异或、~、!运算

    转自:C的|.||.&.&&.异或.~.!运算 位运算     位运算的运算分量只能是整型或字符型数据,位运算把运算对象看作是由二进位组成的位串信息,按位完成指定的运算,得到位 ...