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

题目大意:

有n个商品,m元钱,每件商品的p表示出售价格,q为一个固定价,只有你的余额大于或等于该商品的q价时,你才可以购买该商品,该商品的价值为v,求你可以购买到的最大价值。

题目分析:

一般当商品包含价格与价值(本身条件超过两个)的时候,都是需要将商品按照价格进行排序,然后使用0-1背包进行查找即可,题需要对商品的(q-p)进行排序,这样做的目的好像是为了使前面更新过的值不影响后面更新的状态。

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node{        //用结构体来存放该商品的q,p,v,便于进行排序处理
    int p,q,v;
};
node a[10010];
int f[10010];       //存放购买商品的价值
int cmp(node a,node b)  //将商品的(q-p)值从小到大进行排序
{
    return (a.q-a.p) < (b.q-b.p);
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(f,0,sizeof(f));
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d",&a[i].p,&a[i].q,&a[i].v);
        }
        sort(a,a+n,cmp);
        for(int i=0;i<n;i++)            //0-1背包查找
        {
            for(int j=m;j>=a[i].q;j--)
            {
                f[j]=max(f[j],f[j-a[i].p]+a[i].v);
            }
        }
        printf("%d\n",f[m]);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 14:37:38

hdu 3466 Proud Merchants(0-1背包+排序)的相关文章

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(0-1背包)

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

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

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背包,微变形)

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

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-3466 Proud Merchants(01背包之转移)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3466 题目很容易理解,但是如何将其转换为01背包是个问题. 对物品按 qi-pi 的值从小到大排序,因为这样可以保证每次更新的状态值从小到大递增,前面更新过的状态不会影响后面更新的状态. 题目代码: 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <algorithm&g

HDU 2602 Bone Collector 0/1背包

题目链接:pid=2602">HDU 2602 Bone Collector Bone Collector Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28903    Accepted Submission(s): 11789 Problem Description Many years ago , in Teddy's h

【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