Proud Merchants

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 5
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
 代码:自己原本想的挺对的,越想越复杂了,其实就个排序;题意就是说买东西多了个限制,限制你必须有多少钱才能买;
 

解题思路:与顺序有关的01背包。初看之下似乎和普通背包差不多,判容量大于q时才装。但是这会出大问题,如果一个物品p = 5,q = 7,一个物品p = 5,q = 9,如果先算第一个,那么当次只有7,8...m可以进行状态转移,装第二个物品的时候9,10..m进行转移,第二个物品转移就可以借用第一个物品的那些个状态,而第二个物品先转移,第一个再转移则不能。当然,还有价格有关,当限制一样价格不同时顺序就影响结果。一种组合的排序策略--限制又小价格又贵的先选,也就是q-p小的先选。为什么这样呢?A:p1,q1 B: p2,q2,先选A,则至少需要p1+q2的容量,而先选B则至少需要p2+q1,如果p1+q2>p2+q1,那么要选两个的话的就要先选A再选B,公式可换成q1-p1 > q2-p2,就按这样的方法排序最后的顺序就是最优的顺序。

该题要确保P[i]-Q[i]小的先被”挑选“,差值越小使用它的价值越大(做出的牺牲越小).

代码:

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX(x,y)(x>y?x:y)
const int MAXN=;
const int MAXM=;
struct Node{
int p,q,v;
};
int cmp(const void *a,const void *b){
if((*(Node *)a).q-(*(Node *)a).p<(*(Node *)b).q-(*(Node *)b).p)return -;
else return ;
}
Node dt[MAXM];
int bag[MAXN];
int main(){
int N,M;
while(~scanf("%d%d",&N,&M)){
memset(bag,,sizeof(bag));
for(int i=;i<N;i++)scanf("%d%d%d",&dt[i].p,&dt[i].q,&dt[i].v);
qsort(dt,N,sizeof(dt[]),cmp);
for(int i=;i<N;i++)
for(int j=M;j>=dt[i].q;j--){//
if(j>=dt[i].p){//
bag[j]=MAX(bag[j],bag[j-dt[i].p]+dt[i].v);
}
}
printf("%d\n",bag[M]);
}
return ;
}

Proud Merchants(01背包)的更多相关文章

  1. hdu 3466 Proud Merchants 01背包变形

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

  2. HDU 3466 Proud Merchants(01背包)

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

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

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

  4. HDU 3466 Proud Merchants 排序 背包

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

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

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

  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. hdu 3466 Proud Merchants(有排序的01背包)

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

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

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

  9. HDU 3466 Proud Merchants(01背包)

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

随机推荐

  1. 浏览器兼容问题汇总<转>

    浏览器的内核 Mozilla Firefox ( Gecko ) Internet Explorer ( Trident ) Opera ( Presto ) Safari ( WebKit ) Go ...

  2. SDK Manager 报错:Connection timed out: connect

    安装Eclipse的安卓开发环境的时候,安装sdk时报错,出现: 解决办法: 1.选择左上角的Tools 2.选择Options,勾选下面红色框的东西 3. 4.重新重启一下sdk manager即可

  3. Jenkins的安全控制

    在默认配置下,Jenkins是没有安全检查的.任何人都可以以匿名用户身份进入Jenkins,设置Jenkins和Job,执行build操作.但是,Jenkins在大多数应用中,尤其是暴露在互联网的应用 ...

  4. oracle后台进程2

    oracle中的进程共分为三类:用户进程.服务进程.后台进程.其中后台进程伴随实例的启动而启动,他们主要是维护数据库的稳定,相当于一个企业中的管理者及内部服务人员.他们并不会直接给用户提供服务. 一: ...

  5. JavaScript 回车 焦点切换(摘抄)

    <!-- 这是回车转换行的代码段--> <script language='javascript' for='document' event='onkeydown'> if(e ...

  6. 使用FileSystemWatcher捕获系统文件状态

    源代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...

  7. 图片占位 css

    手机端图片高度和宽度不能自动比例缩小的问题 <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...

  8. zookeeper集群挂了,提示

    Hadoop集群意外关机后,zookeeper服务出现停止状态, 日志错误提示如下: java.io.IOException: Transaction log: //log.260010d366 ha ...

  9. C++中内存分配详解

    转载自51CTO.com           http://developer.51cto.com/art/201107/276154.htm 我们都知道,内存基本上分为静态存储区.堆区和栈区三大部分 ...

  10. 关于jQuery中的ajax的方法介绍

           jQuery提供一系列Ajax函数方便我们调用Ajax, 其中最核心也是最复杂的是jQuery.ajax(),所有的其他Ajax函数都是它的一个简化调用.当我们想要完全控制Ajax时可以 ...