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背包问题 题意 最近,伊萨去了一个古老的国家.在这么长的时间里,它是世界上最富有.最强大的王国.因此,即使他们的国家不再那么富有,这个国 ...
随机推荐
- mysql之innodb_buffer_pool
1>.mysqld重启之后,innodb_buffer_pool几乎是空的,没有任何的缓存数据.随着sql语句的执行,table中的数据以及index 逐渐被填充到buffer pool里面,之 ...
- 异常积累:org.hibernate.StaleStateException
ERROR - Exception executing batch: org.hibernate.StaleStateException: Batch update returned unexpec ...
- 来自“Java中国”优秀的程序员不会觉得累成狗是一种荣耀
分享下“https://java-china.org/topic/28“,也算是对自己的一种告诫吧. 原文:Sleep deprivation is not a badge of honor 先介绍一 ...
- python入门到精通[一]:搭建开发环境
摘要:Python认识,及在windows和linux上安装环境,测试是否安装成功. 1.写在前面 参加工作也有5年多了,一直在做.net开发,近一年有做NodeJS开发.从一开始的不习惯,到逐步适应 ...
- Lintcode: Segment Tree Build
The structure of Segment Tree is a binary tree which each node has two attributes start and end deno ...
- fzu 2146 Easy Game
http://acm.fzu.edu.cn/problem.php?pid=2146 Problem 2146 Easy Game Accept: 661 Submit: 915Time Li ...
- nyist 593 Take it easy
http://acm.nyist.net/JudgeOnline/problem.php?pid=593 Take it easy 时间限制:1000 ms | 内存限制:65535 KB 难度: ...
- iOS 检查版本号的代码
- (void)checkNewVersion{ if ([@"appStore" isEqualToString:CHANNEL]) { AFHTTPRequestOperati ...
- [转] JVM 调优系列 & 高并发Java系列
1.JVM调优总结(1):一些概念:http://www.importnew.com/18694.html 2.JVM调优总结(2):基本垃圾回收算法:http://www.importnew.com ...
- 认真学习shell的第一天-数学运算
shell中的数学运算有三种方式: (1)let,用let的时候,变量名称前不用添加$ (2)[],[]中变量可使用也可不使用$ (3)(())变量名之前必须添加$