Proud Merchants

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

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
题目大意:n中商品,m元钱,每种商品都有p,q,v属性,p价格,q表示买这种商品你需要带q元老板才愿意和你交易,v这种商品的实际价值。求问最多可以获得多少价值
思路:此题对于第二个样例,第一件商品 5 10 5只会把dp[10]更新出来,但实际上花费了5,更新第二个商品时,需要dp[10]=max(dp[10-5]+6,dp[10]),此时需要借助上一层的dp[5],但实际上此时dp[5]还没有更新。所以实际上对于一个p,q他最小能跟新出dp[q-p],所以需要对每件商品安装q-p大小排序,然后再背包求解
 #include <cstring>
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int n,m;
struct node
{
int p,q,v;
}a[];
int dp[];
bool cmp(node a,node b)
{
return a.q-a.p<=b.q-b.p;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,,sizeof(dp));
for(i=;i<n;i++)
scanf("%d%d%d",&a[i].p,&a[i].q,&a[i].v);
sort(a,a+n,cmp);
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]);
}
}

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

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

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

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

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

  3. 3466 ACM Proud Merchants 变形的01背包

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意:假设你有M元,已经Pi,Qi,Vi(i为角标,1<i<N),当M>Qi,时才 ...

  4. hdu 3466 Proud Merchants 【限制性01背包】+【贪心】

    题目链接:https://vjudge.net/contest/103424#problem/J 转载于:https://www.bbsmax.com/A/RnJW16GRdq/ 题目大意: 有n个商 ...

  5. [HDOJ3466]Proud Merchants(贪心+01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3466 n个商人,每个商人有一个物品,物品有价格p.价值v还有一个交易限制q.q的意义是假如你现在拥有的 ...

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

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

  7. Proud Merchants---hdu3466(有01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3466 与顺序有关的01背包. 如果一个物品p = 5,q = 7,一个物品p = 5,q = 9,如果 ...

  8. HDU 3466 01背包变形

    给出物品数量N和总钱数M 对于N个物品.每一个物品有其花费p[i], 特殊值q[i],价值v[i] q[i] 表示当手中剩余的钱数大于q[i]时,才干够买这个物品 首先对N个物品进行 q-p的排序,表 ...

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

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

随机推荐

  1. IOS 面试 --- 网络部分

    网络部分 3 做过的项目是否涉及网络访问功能,使用什么对象完成网络功能? 答案:ASIHTTPRequest与NSURLConnection 4 简单介绍下NSURLConnection类及+ sen ...

  2. Codeforces Round #276 (Div. 2) 解题报告

    题目地址:http://codeforces.com/contest/485 A题.Factory 模拟.判断是否出现循环,如果出现,肯定不可能. 代码: #include<cstdio> ...

  3. 《Programming WPF》翻译 第9章 5.默认可视化

    原文:<Programming WPF>翻译 第9章 5.默认可视化 虽然为控件提供一个自定义外观的能力是有用的,开发者应该能够使用一个控件而不用必须提供自定义可视化.这个控件应该正好工作 ...

  4. WebApi 自定义过滤器实现支持AJAX跨域的请求

    我想关于此类话题的文章,大家一搜铺天盖地都是,我写此文的目的,只是对自己学习过程的记录,能对需要的朋友有所帮助,也百感荣幸!!!废话不多说,直接上代码! 客户端:很简单的AJAX请求 <html ...

  5. Spring Boot 启动加载数据 CommandLineRunner

    实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...

  6. UVa 1449 - Dominating Patterns (AC自动机)

    题目大意:给出多个字符串模板,并给出一个文本串,求在文本串中出现最多的模板,输出最多的次数并输出该模板(若有多个满足,则按输入顺序输出). 思路:赤裸裸的 AC自动机,上模板. 代码: #includ ...

  7. postgresql C/C++ API 接口

    1,postgresql学习uri推荐 http://www.php100.com/manual/PostgreSQL8/ http://www.php100.com/manual/PostgreSQ ...

  8. 判断包含字符String.contains

    Java String.contains()方法用法实例教程, 返回true,当且仅当此字符串包含指定的char值序列 java.lang.String.contains() 方法返回true,当且仅 ...

  9. VirtualBox 扩展包卸载或安装失败(VERR_ALREADY_EXISTS)

    最近在卸载VirtualBox出现了无法卸载的错误.提示为Failed to install the extension. The installer failed with exit code 1: ...

  10. 浅谈Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6621566 上一篇文章Android进程间通信 ...