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. C语言学习中容易模糊的一些概念

    1.什么叫分配内存 操作系统把某一块内存空间的使用权利分配给该程序 2.释放内存 操作系统把分配给该程序的内存空间的使用权利收回,该程序就不能再使用这块内存空间 注:释放内存空间并不是把这块内存的数据 ...

  2. Java学习笔记--Swing

    1.创建框架 AWT中Frame类用来描述顶层窗口,在Swing中,这个类的名为JFrame,它从Frame类扩展. JFrame是少数几个在Swing不用绘制在画布上的组件之一,因此,它的修饰部件( ...

  3. AI自动寻路

    1.首先把游戏场景中的物体设为静态 2.选中Window 中的Navigation ,点击Bake进行场景烘焙 3.在需要寻路的游戏对象上添加 NavMeshAgent组件.调整其AgentSize大 ...

  4. ByteArrayOutputStream的用法

    ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型别数组的缓冲区,然后利用ByteArrayOutputStream和ByteArrayInputStream的 ...

  5. 用php理解指针--写给刚刚学习编程的人

    在刚刚学习编程时,可能for循环什么的还是可以理解,但是当学习到指针的时候,课上估计很多人就睡觉去了. 现在用两端php程序说明指针 先写一个简单的,大家都理解下 <?php class tex ...

  6. Bitly缩短网址服务 - Blog透视镜

    网站的网址过长或是含有非英文或数字符号,会导致在BBS或者微网志中分享给好友时,产生很多的不方便,Bitly是个缩短网址服务的网站,提供1个短网址转向指定到长网址,免费使用且提供统计报表,例如本篇文章 ...

  7. GetProcessMemoryInfo API取得进程所用的内存

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms683219(v=vs.85).aspx 例子: https://msdn.mic ...

  8. php用apc实现的临界区 解决并发,资源互斥同步访问

    在面对线程或进程的互斥同步的控制问题时,常用的解决办法是:临界区,互斥锁,信号量 临界区保证在某一时刻只有一个线程能够访问到所需资源的方法. 任何时候,只能至多有一个线程处于临界区中.如果多个线程要求 ...

  9. C#几种截取字符串的方法小结 (摘抄)

    1.根据单个分隔字符用split截取 例如 string st="GT123_1"; string[] sArray=st.split("_"); 即可得到sA ...

  10. json返回数据拼接HTML

    <div class="box-lists">  </div> $.ajax({ url: 'AjaxPage/AjaxHandler.ashx', typ ...