HDU3466 Proud Merchants [背包]
Proud Merchants
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 7536 Accepted Submission(s): 3144
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?
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.
10 15 10
5 10 5
3 10
5 10 5
3 5 6
2 7 3
分析:一开始考虑直接01背包,将限制条件直接换成j>=q[i],但是发现这种思路是错的。显然物品的顺序会对其产生影响,那么就要考虑如何排序。然后试了几种排序发现都不对,还是K_lord和five20大佬讲过以后才明白。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<iomanip>
using namespace std;
const int N=5e3+;
int n,m,dp[N];
struct Node{int p,q,v;}a[N];
bool cmp(Node x,Node y)
{return x.q-x.p<y.q-y.p;}
int main()
{
ios::sync_with_stdio(false);
while(cin>>n>>m){
for(int i=;i<=n;i++)
cin>>a[i].p>>a[i].q>>a[i].v;
memset(dp,,sizeof(dp));
sort(a+,a+n+,cmp);
for(int i=;i<=n;i++)
for(int j=m;j>=a[i].q;j--)
dp[j]=max(dp[j],dp[j-a[i].p]+a[i].v);
cout<<dp[m]<<"\n";}
return ;
}
HDU3466 Proud Merchants [背包]的更多相关文章
- HDU3466 Proud Merchants[背包DP 条件限制]
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
- hdu3466 Proud Merchants(01背包)
https://vjudge.net/problem/HDU-3466 一开始想到了是个排序后的背包,但是排序的策略一直没对. 两个物品1和2,当p1+q2>p2+q1 => q1-p1& ...
- HDU--3466 Proud Merchants (01背包)
题目http://acm.hdu.edu.cn/showproblem.php?pid=3466 分析:这个题目增加了变量q 因此就不能简单是使用01背包了. 网上看到一个证明: 因为如果一个物品是5 ...
- [hdu3466]Proud Merchants
题目描述 Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and po ...
- Proud Merchants(01背包变形)hdu3466
I - Proud Merchants Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- 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 ...
- hdu 3466 Proud Merchants 01背包变形
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
随机推荐
- CCF-20170901
试题编号: 201709-1 试题名称: 打酱油 时间限制: 1.0s 内存限制: 256.0MB 问题描述 小明带着N元钱去买酱油.酱油10块钱一瓶,商家进行促销,每买3瓶送 ...
- c# Stream to File的知识点
个人倾向使用File.WriteAllByte写入文件: //Stream to File MemoryStream ms=...Stream; ms.Position = ; byte[] buff ...
- @JsonField 修改json字段属性名称
在前后端分离的开发方式中,经常会遇到后端字段名称和前端字段名称定义不一致的问题,比如,后端定义的Bean中的字段名称为createAt,而前端用的字段名称为createTime.这种情况下可以通过在前 ...
- Android蓝牙通信总结
这篇文章要达到的目标: 1.介绍在Android系统上实现蓝牙通信的过程中涉及到的概念. 2.在android系统上实现蓝牙通信的步骤. 3.在代码实现上的考虑. 4.例子代码实现(手持设备和蓝牙串口 ...
- 「6月雅礼集训 2017 Day8」gcd
[题目大意] 定义times(a, b)表示用辗转相除计算a和b的最大公约数所需步骤. 那么有: 1. times(a, b) = times(b, a) 2. times(a, 0) = 0 3. ...
- IBM InfoSphere DataStage and QualityStage
Info coms from https://www.ibm.com/support/knowledgecenter/en/SSZJPZ_9.1.0/com.ibm.swg.im.iis.ds.nav ...
- Python模块学习 - openpyxl
openpyxl模块介绍 openpyxl模块是一个读写Excel 2010文档的Python库,如果要处理更早格式的Excel文档,需要用到额外的库,openpyxl是一个比较综合的工具,能够同时读 ...
- Python模块学习 - Fileinput
Fileinput模块 fileinput是python提供的标准库,使用fileinput模块可以依次读取命令行参数中给出的多个文件.也就是说,它可以遍历 sys.argv[1:],并按行读取列表中 ...
- python基础===解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX(转载)
本文转自:解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX 从网上抓了一些字 ...
- Makefile系列之四 :条件判断
一.示例 下面的例子,判断$(CC)变量是否“gcc”,如果是的话,则使用GNU函数编译目标. libs_for_gcc = -lgnu normal_libs = foo: $(objects) i ...