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 ;
}

最新文章

  1. Linux(十)___iptables防火墙
  2. SQLServer比较两条数据是否相同
  3. android 在线升级借助开源中国App源码
  4. [ html canvas 模仿支付宝刮刮卡效果 ] canvas绘图属性 模仿支付宝刮刮卡效果实例演示
  5. iOS 架构模式-MVVM
  6. Ubuntu 安装
  7. 1 ubuntu下装setuptools
  8. Codeforces Round #347 (Div. 2) C. International Olympiad 找规律
  9. [译]深入理解JVM
  10. IE8下实现兼容rgba
  11. MySQL 笔记整理(11) --怎么给字符串字段加索引?
  12. Django Linux环境下部署CentOS7+Python3+Django+uWSGI+Nginx(含Nginx返回400问题处理、防火墙管理)
  13. RocketMQ 简单梳理 及 集群部署笔记
  14. springboot项目中jdk版本的问题
  15. 转 linux常用查看硬件设备信息命令
  16. js中 xpath 使用
  17. 安装 directx sdk 出现 S1023 解决
  18. 菜鸟学Java(十七)——Jboss瘦身
  19. Mac 下安装node.js(转载)
  20. android线程学习心得

热门文章

  1. 使用AES加密的帮助类
  2. c++ 17介绍
  3. 2014.06.20 (转)IEEE与论坛灌水
  4. HDU2114 Calculate S(n) (取模算术)
  5. UML_部署图
  6. tomcat root dir log 配置
  7. Python学习笔记10-Python MysqlHelper ,MySql 辅助类
  8. 安装android studio时,解决unable to access android sdk add-on list
  9. MD5和SHA512Managed ——哈希算法
  10. Spring Http Invoker