Proud Merchants 3466 (01背包+排序+技巧)
2015-08-26 20:56
447 查看
Proud Merchants
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 3608 Accepted Submission(s): 1505
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, output one integer, indicating maximum value iSea could get.
[align=left]Sample Input[/align]
2 10
10 15 10
5 10 5
3 10
5 10 5
3 5 6
2 7 3
[align=left]Sample Output[/align]
5
11
搜索
//题意: //小明去古董城买古董,但古董城有个规矩,如果你手里的钱少于商家给的出手价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; }
相关文章推荐
- [图论训练]BZOJ 2118: 墨墨的等式 【最短路】
- for循环输出<国际象棋>
- 饭卡
- 网络篇 - 02.NSURLConnection基本使用
- HDU1285 - 确定比赛名次 拓补排序
- hdu 1869 六度分离
- NandFlash驱动设计
- 编程控制分屏显示
- 消夏
- cocos2dx 玩转标签Label类
- 高薪下的互联网人为何不开心
- gdb调试Android-5.1版本JNI模块
- 2.8-PHP.in配置文件详解
- 新锐国际黑心外包商,拖欠员工工资,社保、公积金
- pdf2htmlEX将pdf文件转化为html最优参数以及把生成文件等比例缩小
- Oracle Workflow Demo (1) - 一个简单的请假申请工作流
- 背包问题——“01背包”详解及实现(包含背包中具体物品的求解)
- C++ 中 dynamic_cast 使用(二)
- Gas Station
- 背包问题(课件内容(讲解,容易理解))