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. ERROR Shell: Failed to locate the winutils binary in the hadoop binary path

    文章发自:http://www.cnblogs.com/hark0623/p/4170172.html  转发请注明 14/12/17 19:18:53 ERROR Shell: Failed to ...

  2. C# 键值对类相关

    一 C# 键值对类有以下类: ①    IDictionary<string, Object> idc = new Dictionary<string, object>(); ...

  3. Java命令行输入求和的简单学习

    思想:命令行输入的参数,必须先转换为数字才能进行加法计算,这就需要引用java.util.Scanner; 流程框图: 源代码: //实现几个整数相加的程序 //高开拓,2015.9.26 packa ...

  4. MFC CString to char* (Visual Studio 2015 亲测可用)

    右键划线部分,打开属性,然后找到:配置属性-常规-字符集,选择:使用多字节字符集 然后,char *c;CString str; 就可以 strcpy(c,str); 如果觉得好用,请点个赞,谢谢!

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

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

  6. Oracle查询每天固定时间段的数据

    select * from GPS_LOG t where to_char(t.gps_time,'hh24:mm:ss')>='15:30:00'and to_char(t.gps_time, ...

  7. The 2015 China Collegiate Programming Contest E. Ba Gua Zhen hdu 5544

    Ba Gua Zhen Time Limit: 6000/4000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  8. 桌面每日一句--桌面翻译工具(有道翻译,微软翻译,Google翻译)

    现在的翻译软件功能越来越多,也越来越臃肿,还不时弹广告,真的很烦恼. 鉴于这种情况,自己做了个翻译软件,能满足日常需求就好了.需要用的时候可以直接在桌面输入单词翻译,或者直接使用快捷键呼出翻译窗口.最 ...

  9. Spoj 10628. Count on a tree 题解

    题目大意: 给定一棵n个点的树,每个点有一个权值,m个询问,每次询问树上点x到点y的路径上的第k小数. 思路: dfs后给每个节点一个dfs序,以每个点在他父亲的基础上建立主席树,询问时用(点x+点y ...

  10. ACM: 强化训练-Beautiful People-最长递增子序列变形-DP

    199. Beautiful People time limit per test: 0.25 sec. memory limit per test: 65536 KB input: standard ...