Proud Merchants

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

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
 
题意: 购买物品,pi qi vi  (pi代表要花的钱,qi代表你当前的钱要大于等于这个值才能买,vi 价值了。)
      和单纯的01背包比较,多了qi,在这里就发生区别了。
          for(j=sum_money;j>=f[i].qi&&(j-f[i].vi)>=0;j--)
    采取的方式是 对 qi-pi进行从小到大排序,使得差值 越小的越先放。
         
    为什么要这样排序呢?动手画一下。详细的证明,以后再写。
         
          其实单纯的01背包它的差值就是0,所以还是有共性的。
 #include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std; struct node
{
int pi,qi,vi;
}f[];
int dp[]; bool cmp(node n1,node n2)
{
return (n1.qi-n1.pi)<(n2.qi-n2.pi);
} int main()
{
int n,sum_money,max;
int i,j,tmp;
while(scanf("%d%d",&n,&sum_money)>)
{
for(i=;i<=n;i++)
{
scanf("%d%d%d",&f[i].pi,&f[i].qi,&f[i].vi);
}
sort(f+,f++n,cmp);
memset(dp,,sizeof(dp)); for(i=;i<=n;i++)
{
for(j=sum_money;j>=f[i].qi&&(j-f[i].pi)>=;j--)
{
tmp=dp[j-f[i].pi]+f[i].vi;
if(tmp>dp[j])
dp[j]=tmp;
}
}
printf("%d\n",dp[sum_money]);
}
return ;
}
 
 
 
 
 

hdu 3466 Proud Merchants 01背包变形的更多相关文章

  1. HDU 3466 Proud Merchants(01背包)

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

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

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

  3. HDU 3466 Proud Merchants 排序 背包

    题意:物品有三个属性,价格p,解锁钱数下线q(手中余额>=q才有机会购买该商品),价值v.钱数为m,问购买到物品价值和最大. 思路:首先是个01背包问题,但购买物品受限所以应先排序.考虑相邻两个 ...

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

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

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

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

  6. hdu 3466 Proud Merchants(有排序的01背包)

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

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

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

  8. HDU 3466 Proud Merchants(01背包)

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

  9. Proud Merchants(01背包)

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

随机推荐

  1. 【maven】---聚合和继承

    前言 自从我知道写maven实战这本书的作者长得随心所欲后,我再拿起这本书真心的不想看前言了.下面分享一下maven中的所谓的聚合和继承. 内容 下文中的子本指的是:多个maven项目. 父本指的是: ...

  2. [ActionScript 3.0] 使用Embed在类中嵌入字体

    package { import flash.display.Sprite; import flash.text.Font; import flash.text.TextField; import f ...

  3. 重置root密码

    一. ubuntu忘记密码解决方法 1. 开机按shift 2. 选择高级选项,进入后选择恢复模式(不要按回车),按e进入编辑模式 3. 修改linux命令中的recovery nomodest 为 ...

  4. python3入门之集合set

    之前介绍python的数据结构时,没有介绍set(集合)现在在这里稍微介绍下: set原理 Python 还 包 含 了 一 个 数 据 类 型-- set ( 集 合 ) . 集 合 是 一 个 无 ...

  5. android 微信 SDK 分享

    1.资源依赖: https://open.weixin.qq.com/cgi-bin/showdocument?action=doc&id=open1419319167&t=0.613 ...

  6. java.lang.IllegalArgumentException: invalid comparison: java.util.Date and java.lang.String

    在重构项目的时候,遇到了mybatis的一个异常: java.lang.IllegalArgumentException: invalid comparison: java.util.Date and ...

  7. SAP Sybase IQ 操作基础

    1.启动 source IQ-16_0.sh 命令行查看安装程序是否成功 start_iq -v2 2.数据库.表空间 start_iq -n utility_db dbisql -c 'uid=db ...

  8. 移远EC20的使用

    一 发短信 3. 推荐短信流程3.1 查询 短信存储区AT+CPMS?+CPMS: "ME",19,255,"ME",19,255,"ME" ...

  9. String字符串排序1.8 lamda表达式以及1.7自定义排序

    // 1.8 public class text { public static void main(String[] args) { String s1 = "哈哈哈11,呵呵呵22,嘿嘿 ...

  10. (转)heartbeat原理及部署

    原文:http://yjy724.blog.51cto.com/10897133/1840794---------------------------------------------------h ...