HDU3466 背包DP

Proud Merchants

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

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

题意:

买一件价格为p的物品时手里的钱不少于p时才能购买。给出n件物品的p,q,v,v代表价值,总钱数m,问最大能够买到多大的价值。

代码:

//先买q大而p小的比较好,所以按照q-p排序,注意是从小到大排序,因为背包公式中
//f[i]=max(f[i],f[i-m]+v)是假设i-m被装满时再装下一件,是假设倒着装的。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int f[5005];
int n,m;
struct proud
{
    int p,q,v,ch;
};
bool cmp(proud a,proud b)
{
    return a.ch<b.ch;
}
/*void zeroonepack(int v,int m,int ttl)
{
    for(int i=ttl;i>=v;i--)
    f[i]=max(f[i],f[i-v]+m);
}
void complitpack(int v,int m,int ttl)
{
    for(int i=v;i<=ttl;i++)
    f[i]=max(f[i],f[i-v]+m);
}
void multipack(int v,int m,int c,int ttl)
{
    if(c*v>=ttl)
    complitpack(v,m,ttl);
    else
    {
        int k=1;
        while(k<c)
        {
            zeroonepack(k*v,k*m,ttl);
            c-=k;
            k*=2;
        }
        zeroonepack(c*v,c*m,ttl);
    }
}*/
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(f,0,sizeof(f));
        proud pr[505];
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d",&pr[i].p,&pr[i].q,&pr[i].v);
            pr[i].ch=pr[i].q-pr[i].p;
        }
        sort(pr+1,pr+n+1,cmp);
        for(int i=1;i<=n;i++)
        {
            for(int j=m;j>=pr[i].q;j--)
            {
                f[j]=max(f[j],f[j-pr[i].p]+pr[i].v);
            }
        }
        int ans=0;
        printf("%d\n",f[m]);
    }
    return 0;
}
时间: 2024-10-08 10:45:08

HDU3466 背包DP的相关文章

HDU3466——背包DP——Proud Merchants

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.

[BZOJ 1025] 游戏 置换群 背包DP

题意 对于一个 $n$ 阶置换群 $A$ , 它的循环节大小分别为 $a_1, a_2, ..., a_m$ , 则有 $\sum_{i = 1} ^ m a_i = n$ . 定义 $f(A)$ 为它的所有循环节的最小公倍数, 即 $f(A) = [a_1, a_2, ..., a_m]$ . 求在所有 $n$ 阶置换群中, $f(A)$ 有多少种取值. $n \le 1000$ . 分析 判断 $K$ 可不可取. $K = \prod_{i = 1} ^ r {s_r} ^ {t_r}$ 可

hdu 5234 Happy birthday 背包 dp

Happy birthday Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5234 Description 今天是Gorwin的生日.所以她的妈妈要实现她的一个愿望.Gorwin说她想吃很多蛋糕.所以他妈妈带她来到了蛋糕园. 这个园子被分成了n*m个方格子.在每一个格子里面,有一个蛋糕.第i行,第j列的格子中有一个重量为wij千克的蛋糕,Gorwin从左上角(1,1

hdu 1171 Big Event in HDU(背包DP)

题意: 杭电搬迁,有N种设备,每种设备有个价值V,数量M,要求将这些设备平分,使得平分后两边的总价值尽可能地相等. 输出两边各自的总价值. 思路: 背包DP后,P=所有的总价值/2,然后从P开始往两边找到第一个满足的价值. 可以降维,但是要注意for循环的顺序. 看代码. 代码: int v[55], m[55]; bool dp[250005]; int main(){ int n; while(scanf("%d",&n)!=EOF && n>=0){

POJ 1384 Piggy-Bank 背包DP

所谓的完全背包,就是说物品没有限制数量的. 怎么起个这么intimidating(吓人)的名字? 其实和一般01背包没多少区别,不过数量可以无穷大,那么就可以利用一个物品累加到总容量结尾就可以了. 本题要求装满的,故此增加个限制就可以了. #include <stdio.h> #include <stdlib.h> #include <string.h> inline int min(int a, int b) { return a < b? a : b; } c

BZOJ 1042 硬币购物(完全背包+DP)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1042 题意:给出四种面值的硬币c1,c2,c3,c4.n个询问.每次询问用d1.d2.d3.d4个相应的硬币能够拼出多少种总和为s? 思路:(1)首先,用完全背包求出f[i]表示四种硬币的数量无限制拼出i的方案数. (2)接着我们来理解 x=f[s]-f[s-(d1+1)*c1]的含义:x表示c1硬币的数量不超过d1个而其他三种硬币的数量不限制拼成s的方案数.我们举着例子来说明, 假设

HDU 5616 Jam&#39;s balance 背包DP

Jam's balance Problem Description Jim has a balance and N weights. (1≤N≤20)The balance can only tell whether things on different side are the same weight.Weights can be put on left side or right side arbitrarily.Please tell whether the balance can me

hdu1561:树形背包dp

给定n个地点,每个地点藏有cost[i]的宝物,取得某些宝物有时需要先取其他宝物,现在让我们选m个地点问最多可以选多少宝物? 还是挺裸的树形背包dp吧,不难,关键还是中间dp的部分.可以做模板了->_-> 注意点:多组数据的话如果第一组对了然后其他都错了,那么很有可能是初始化的时候漏了.这次找可很久才知道差了e[0].clear().平时的习惯都是从1开始. --------------------------------------------------------------------

背包DP HDOJ 5410 CRB and His Birthday

题目传送门 题意:有n个商店,有m金钱,一个商店买x件商品需要x*w[i]的金钱,得到a[i] * x + b[i]件商品(x > 0),问最多能买到多少件商品 01背包+完全背包:首先x == 1时,得到a[i] + b[i],若再买得到的是a[i],那么x == 1的情况用01背包思想,x > 1时就是在01的基础上的完全背包.背包dp没刷过专题,这么简单的题也做不出来:( /************************************************* Author