Proud Merchants
Proud Merchants
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 3557 Accepted Submission(s): 1479
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
按照q-p的大小进行排序,再进行01背包,至于为什么这样排序,网上的题解没有看懂,在我看来,普通的01背包每次的状态转移都是基于前一个状态,所以只有尽量减少状态的损失才可以得到最优解,而状态损失量就q-p
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef pair<int,int>p;
const int INF = 0x3f3f3f3f;
struct node
{
int p;
int q;
int v;
int dis;
}Th[550];
int Dp[5010];
int n,m;
bool cmp(node a,node b)
{
return a.dis<b.dis;
}
int main()
{
while(~scanf("%d %d",&n,&m))
{
for(int i=0;i<n;i++)
{
scanf("%d %d %d",&Th[i].p,&Th[i].q,&Th[i].v);
Th[i].dis=Th[i].q-Th[i].p;
}
memset(Dp,0,sizeof(Dp));
sort(Th,Th+n,cmp);
for(int i=0;i<n;i++)
{
for(int j=m;j>=Th[i].q;j--)
{
Dp[j]=max(Dp[j],Dp[j-Th[i].p]+Th[i].v);
}
}
printf("%d\n",Dp[m]);
}
return 0;
}
Proud Merchants的更多相关文章
- HDU3466 Proud Merchants[背包DP 条件限制]
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- HDU 3466 Proud Merchants(01背包问题)
题目链接: 传送门 Proud Merchants Time Limit: 1000MS Memory Limit: 65536K Description Recently, iSea wen ...
- Proud Merchants(POJ 3466 01背包+排序)
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- Proud Merchants(01背包)
Proud Merchants Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- HDU 3466 Proud Merchants(01背包)
这道题目看出背包非常easy.主要是处理背包的时候须要依照q-p排序然后进行背包. 这样保证了尽量多的利用空间. Proud Merchants Time Limit: 2000/1000 MS (J ...
- Proud Merchants(01背包变形)hdu3466
I - Proud Merchants Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- hdu 3466 Proud Merchants 01背包变形
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- HDU3466 Proud Merchants [背包]
题目传送门 Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/O ...
- HDU 3466 Proud Merchants 带有限制的01背包问题
HDU 3466 Proud Merchants 带有限制的01背包问题 题意 最近,伊萨去了一个古老的国家.在这么长的时间里,它是世界上最富有.最强大的王国.因此,即使他们的国家不再那么富有,这个国 ...
随机推荐
- JDK JVM
- Effective C++ 2.构造 析构 赋值运算
//条款07:为多态基类声明virtual析构函数 // 1.若基类的析构函数不定义为虚函数,由于基类的指针或引用可以指向派生类的对象,则在删除基类对象的时候可能会出错,导致破坏数据结构. // 2. ...
- Swift游戏实战-跑酷熊猫 11 欢迎进入物理世界
物理模拟是一个奇妙的事情,以此著名的游戏有愤怒的小鸟.我们在这节将会一起来了解如何设置重力,设置物理包围体,碰撞的检测. 要点: 设置物理检测的代理: 让主场景遵循SKPhysicsContactDe ...
- Codeforce Round #228 Div2
这次的A题没注意要到100- -, B题没做,后来做要注意下1和long long C题当时坑的一B,用了个蠢办法,后来还错了,现在改了,还是蠢办法,等等再去用dp吧,而且本来就只有01用个鸡巴的树状 ...
- oracle 新手遇到常见问题的解决办法
可能照成以下问题的原因也许有很多种,但是就小白而言,我只记录自己学习过程中遇到的所有的问题.希望对一些新手 小白们有所帮助. 原因是 sys 不是sysdba 用户,你要将其作为sysdba 用户登录 ...
- C++之路进阶——codevs1204(寻找子串位置)
1204 寻找子串位置 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 青铜 Bronze 题目描述 Description 给出字符串a和字符串b,保证b是a的一个子 ...
- ORA-12154的原因
一个很难想到的引起ORA-12154的原因 使用OracleClient.OracleConnection时(我连的是Oracle 9i,其他版本未知),如果你的执行目录太长或者有括号 ...
- oracle的索引维护
索引重建 Alter index idx_name rebuild partition index_partition_name [online nologging] 需要对每个分区索引做rebuil ...
- 搞不定linux下的无线网卡驱动的权宜之计
毕竟windows用了这么些年了,对windows下的一些东西也比较熟悉,还有就是windows的软件方式比较傻瓜. 在linux下搞不定无线网卡啊,幸亏有甲骨文的virtualbox,咱虚拟一个xp ...
- Workspace Cloning / Sharing in Jenkins
http://lwandersonmusings.blogspot.com/2011/06/workspace-cloning-sharing-in-hudson.html What's insi ...