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
 
题意:
买一件价格为p的物品时手里的钱不少于q时才能购买。给出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[];
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,,sizeof(f));
proud pr[];
for(int i=;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+,pr+n+,cmp);
for(int i=;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=;
printf("%d\n",f[m]);
}
return ;
}

HDU3466 背包DP的更多相关文章

  1. 背包dp整理

    01背包 动态规划是一种高效的算法.在数学和计算机科学中,是一种将复杂问题的分成多个简单的小问题思想 ---- 分而治之.因此我们使用动态规划的时候,原问题必须是重叠的子问题.运用动态规划设计的算法比 ...

  2. hdu 5534 Partial Tree 背包DP

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  3. HDU 5501 The Highest Mark 背包dp

    The Highest Mark Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...

  4. Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp

    B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...

  5. noj [1479] How many (01背包||DP||DFS)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...

  6. HDU 1011 树形背包(DP) Starship Troopers

    题目链接:  HDU 1011 树形背包(DP) Starship Troopers 题意:  地图中有一些房间, 每个房间有一定的bugs和得到brains的可能性值, 一个人带领m支军队从入口(房 ...

  7. BZOJ 1004: [HNOI2008]Cards( 置换群 + burnside引理 + 背包dp + 乘法逆元 )

    题意保证了是一个置换群. 根据burnside引理, 答案为Σc(f) / (M+1). c(f)表示置换f的不动点数, 而题目限制了颜色的数量, 所以还得满足题目, 用背包dp来计算.dp(x,i, ...

  8. G - Surf Gym - 100819S -逆向背包DP

    G - Surf Gym - 100819S 思路 :有点类似 逆向背包DP , 因为这些事件发生后是对后面的时间有影响. 所以,我们 进行逆向DP,具体 见代码实现. #include<bit ...

  9. 树形DP和状压DP和背包DP

    树形DP和状压DP和背包DP 树形\(DP\)和状压\(DP\)虽然在\(NOIp\)中考的不多,但是仍然是一个比较常用的算法,因此学好这两个\(DP\)也是很重要的.而背包\(DP\)虽然以前考的次 ...

随机推荐

  1. 关于初级java面试问题的一些整理 (部分转自他人)

    1.面向对象的特征有哪些方面       1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部 ...

  2. blade快速使用指南

    一.简介模板引擎 模板引擎是将网站的页面设计和PHP应用程序几乎完全分离的一种解决方案,它能让前端工程师专注页面搭建,让后台工程师专注功能实现,以便实现逻辑分离,让每个人发挥所长.模板引擎技术的核心是 ...

  3. 8611 大牛之路I

    时间限制:500MS  内存限制:1000K 提交次数:157 通过次数:62 题型: 编程题   语言: C++;C Description 要成为ACM大牛,要掌握很多必需的知识点.某些知识点可以 ...

  4. wpf 背景镂空loading.....

    第一步,,使用arc控件 ArcThickness="15" StartAngle="-6" EndAngle="6" 2,拉一个Ellip ...

  5. [bzoj4514]数字配对[费用流]

    今年SDOI的题,看到他们在做,看到过了一百多个人,然后就被虐惨啦... 果然考试的时候还是打不了高端算法,调了...几天 默默地yy了一个费用流构图: 源连所有点,配对的点连啊,所有点连汇... 后 ...

  6. UI中经常出现的下拉框下拉自动筛选效果的实现

    小需求是当你在第一个下拉框选择了国家时,会自动更新第二个省份的下拉框,效果如下 两个下拉选择Html如下: <select id="country_select"> & ...

  7. iOS UIImageView用代码添加点击事件

    image.userInteractionEnabled = YES; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]ini ...

  8. Codeforces 528D Fuzzy Search(FFT)

    题目 Source http://codeforces.com/problemset/problem/528/D Description Leonid works for a small and pr ...

  9. HDU5772 String problem(最大权闭合子图)

    题目..说了很多东西 官方题解是这么说的: 首先将点分为3类 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分) 第二类:原串中的n个点每个 ...

  10. 每天一个linux命令---导出到文件

    导出Linux下的部分日志到文件,使用‘>’符号 例如: [calendar@test190 logs]$ monitor.log|grep getCalendarView > share ...