Proud Merchants 3466 (01背包+排序+技巧)
来源:互联网 发布:淘宝马切达是什么牌子 编辑:程序博客网 时间:2024/06/08 17:52
Proud Merchants
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 3608 Accepted Submission(s): 1505
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?
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.
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 1010 15 105 10 53 105 10 53 5 62 7 3
Sample Output
511
搜索
//题意://小明去古董城买古董,但古董城有个规矩,如果你手里的钱少于商家给的出手价Q,即使你的钱可以//买下这个古董,但商家也不会卖给你,因为你没有出手的资本。//小明是个古董行家,他能评估出古董的真正价值 //现在有n件古董,小明有m元, //每件古董价格为 P ,出售价为 Q ,古董的真正价格为 V 。//计算小名可以获得的最大的商品的真正价值是多少 //解题思路:://大神写的,,很详细 //首先可以肯定的是,这是一个01背包问题,但要对其进行一定的处理// 比如A:p1 q1, B:p2 q2,然后,假设单独买A或者B的话,都是可以买到的。这时,若先买A,//则你至少需要p1+q2的钱;若先买B,则至少需要p2+q1的钱。那肯定是花最少的钱咯,所以如//果先买A再买B,那么p1+q2<p2+q1,转换一下,就是q1-p1>q2-p2,也就是说qi-pi大的先买。//这里还得注意一点就是,排序的时候,得按照qi-pi从小到大排序,因为你买第n件商品的时候,//是在比较你是否要先买第n件商品。打个比方让大家更好地理解,比如说f(3, 10),是不是//max(f(2, 10-p3)+v3, f(2, 10)),你会发现这个第一种情况f(2,10-p3)+v3中,是先买了第三件商品,//也就是说排在后面的商品会先买。好的,排好序之后,就把问题就转换为不需要考虑顺序的问题了,//那就是上面我们已经解决0/1背包问题了。这样,问题圆满解决了。#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int sum[10100];struct zz{int p,q,v;}a[100010];int cmp(zz a,zz b){return a.q-a.p<b.q-b.p;}int main(){int n,m,i,j;while(scanf("%d%d",&n,&m)!=EOF){memset(sum,0,sizeof(sum));for(i=0;i<n;i++)scanf("%d%d%d",&a[i].p,&a[i].q,&a[i].v);sort(a,a+n,cmp);for(i=0;i<n;i++){for(j=m;j>=a[i].p&&j>=a[i].q;j--){sum[j]=max(sum[j],sum[j-a[i].p]+a[i].v);}}printf("%d\n",sum[m]);}return 0;}
0 0
- Proud Merchants 3466 (01背包+排序+技巧)
- hdu3466 Proud Merchants(排序+01背包)
- hdu 3466 Proud Merchants(先排序的01背包)
- HDU 3466 Proud Merchants(排序的01背包)@
- HDU 3466 Proud Merchants(01背包+排序)
- HDU 3466 Proud Merchants (01背包排序问题)
- HDU 3466 Proud Merchants(01背包)
- hdu 3466 Proud Merchants(01背包)
- HDOJ 3466 Proud Merchants(01背包)
- hdu 3466 Proud Merchants (01背包)
- Proud Merchants(01背包)
- hdu 3466(排序+背包)Proud Merchants
- hdu 3466 Proud Merchants <背包+sort排序>
- hdu3466 Proud Merchants 01背包 排序 dp
- HDU3466 Proud Merchants 排序01背包
- Proud Merchants 。。。01背包
- HDOJ 题目3466 Proud Merchants(需排序的01背包)
- HDU 3466 Proud Merchants(01背包/按limit-price从小到大排序)
- gdb调试Android-5.1版本JNI模块
- cocos2dx 玩转标签Label类
- 背包问题——“01背包”详解及实现(包含背包中具体物品的求解)
- for循环输出<国际象棋>
- HDU1285 - 确定比赛名次 拓补排序
- Proud Merchants 3466 (01背包+排序+技巧)
- hdu 1869 六度分离
- 网络篇 - 02.NSURLConnection基本使用
- hdoj.1131 Count the Trees【卡特兰数列+阶乘】 2015/08/26
- NandFlash驱动设计
- Linux登陆或者su - 用户慢问题
- Java IO流->处理流->标准输入输出流:System.in&System.out
- 内存缓冲区fflush(stdin)
- JavaScript内核笔记02-对象、原型、this指针