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

Proud Merchants

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

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

2010
ACM-ICPC Multi-University Training Contest(3)——Host by WHU

Recommend

zhouzeyong   |   We have carefully selected several
similar problems for you:  3460 3463 3468 3467 3465

简单的01背包,排个序就好,代码很简单。

复制一下网上大神的思路:

因为如果一个物品是5 9,一个物品是5 6,对第一个进行背包的时候只有dp[9],dp[10],…,dp[m],再对第二个进行背包的时候,如果是普通的,应该会借用前面的dp[8],dp[7]之类的,但是现在这些值都是0,所以会导致结果出错。于是要想到只有后面要用的值前面都可以得到,那么才不会出错。设A:p1,q1 B:p2,q2,如果先A后B,则至少需要p1+q2的容量,如果先B后A,至少需要p2+q1的容量,那么就是p1+q2 > p2+q1,变形之后就是q1-p1 < q2-p2。

所以要针对每个属性的q-p来进行排序

题意:每个物品有p、q、v,三个属性,每个物品的话费为p,但是前提是必须有q,v则是得到的价值。

附上代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 struct node
 7 {
 8     int p,q,v;
 9 } ss[505];
10
11 int maxs(int a,int b)
12 {
13     return a>b?a:b;
14 }
15 bool cmp(node a,node b)
16 {
17     return a.q-a.p<b.q-b.p;  //关键排序,详见上文分析
18 }
19 int main()
20 {
21     int n,m,i,j;
22     int dp[5005];
23     while(~scanf("%d%d",&n,&m))
24     {
25         for(i=0; i<n; i++)
26             scanf("%d%d%d",&ss[i].p,&ss[i].q,&ss[i].v);
27         sort(ss,ss+n,cmp);
28         memset(dp,0,sizeof(dp));
29         for(i=0; i<n; i++)
30             for(j=m; j>=ss[i].q; j--)
31                 dp[j]=maxs(dp[j],dp[j-ss[i].p]+ss[i].v);
32         printf("%d\n",dp[m]);
33     }
34     return 0;
35 }
时间: 2024-08-26 22:29:46

hdu 3466 Proud Merchants(有排序的01背包)的相关文章

hdu 3466 Proud Merchants(0-1背包+排序)

题目来源:hdu 3466 Proud Merchants Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 3595 Accepted Submission(s): 1500 Problem Description Recently, iSea went to an ancient country. For

hdu 3466 Proud Merchants &lt;背包+sort排序&gt;

Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 3616    Accepted Submission(s): 1511 Problem Description Recently, iSea went to an ancient country. For such a long time, it was

HDU 3466 Proud Merchants(01背包)

这道题目看出背包很容易,主要是处理背包的时候需要按照q-p排序然后进行背包. 这样保证了尽量多的利用空间. Proud Merchants Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submission(s): 2674    Accepted Submission(s): 1109 Problem Description Recently, iSea we

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

题意: 要买一些东西,每件东西有价格和价值,但是买得到的前提是身上的钱要比该东西价格多出一定的量,否则不卖.给出身上的钱和所有东西的3个属性,求最大总价值. 思路: 1)WA思路:与01背包差不多,dp过程中记录每个容量所能获得的最大价值以及剩余的容量.实现是,开个二维dp数组,从左往右扫,考虑背包容量为j的可获得价值量,根据该剩余容量得知要更新后面哪个背包容量[j+?] ,如果剩余容量大于q[i]那么可以直接装进去,更新价值以及剩余容量,否则,考虑更新的是更大的背包容量.这个思路有缺陷,一直找

HDU 3466 Proud Merchants(0-1背包)

http://acm.hdu.edu.cn/showproblem.php?pid=3466 题意: 最近,iSea去了一个古老的国家.在这么长的时间里,它是世界上最富有和最强大的王国.结果,这个国家的人民仍然非常自豪,即使他们的国家没有那么富有了.商人是最典型的,每个人只卖一个项目,价格是Pi,但如果你的钱少于Qi,他们会拒绝与你交易,iSea评估每个项目一个值Vi.如果他有M单位的钱,iSea可以获得的最大价值是多少? 思路: 这道题的话多加了一个Qi. 一定要注意,若要保证动归方程无后效性

hdu 3466 Proud Merchants 【限制性01背包】

题目链接:https://vjudge.net/contest/103424#problem/J 转载于:https://www.bbsmax.com/A/RnJW16GRdq/ 题目大意: 有n个商品m块钱,给出买每个商品所花费的钱P.钱包里需要至少Q才有资格买.商品的价值V.问你如何购买商品的价值最大. 解题分析:考虑下面的例子:A:p1=5, q1=10, v1=5; B:p2=3, q2=5, v2=6; 如果先买物品A再买物品B的话我至少需要10元钱,也就是money >= p1+q2

HDU 3033 I love sneakers! (DP 01背包+完全背包)

Problem Description After months of hard working, Iserlohn finally wins awesome amount of scholarship. As a great zealot of sneakers, he decides to spend all his money on them in a sneaker store. There are several brands of sneakers that Iserlohn wan

【HDOJ】3466 Proud Merchants

先排序预处理,后01背包. 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define MAX(a, b) (a>b) ? a:b 6 7 int dp[5000]; 8 9 typedef struct { 10 int p, q, v; 11 } stuff_st; 12 13 stuff_st stuffs[505]; 14 15 int comp(const vo

HDU 5887 Herbs Gathering(搜索求01背包)

http://acm.hdu.edu.cn/showproblem.php?pid=5887 题意: 容量很大的01背包. 思路: 因为这道题目背包容量比较大,所以用dp是行不通的.所以得用搜索来做,但是需要一些剪枝,先按体积排序,优先考虑体积大的物品,这样剪枝会剪得多一些(当然按照性价比排序也是可以的). 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdi