HDU3466 Proud Merchants[背包DP 条件限制]
Proud Merchants
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 5599 Accepted Submission(s): 2362
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
11
题意:n件物品有m元,物品价格p,价值q,条件是手里多于q元才能买,求最大价值
按q-p小到大排序然后01背包
A:p1,q1 B: p2,q2,先选A,则至少需要p1+q2的容量,而先选B则至少需要p2+q1,如果p1+q2>p2+q1,那么要选两个的话的就要先选A再选B,公式可换成q1-p1 < q2-p2,
就按照取出两个比较的套路来行了
//
// main.cpp
// hdu3466
//
// Created by Candy on 29/10/2016.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N=,M=;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m;
struct item{
int p,q,w;
bool operator <(const item &r)const{return q-p<r.q-r.p;}
}a[N];
int f[M];
void dp(){
memset(f,,sizeof(f));
for(int i=;i<=n;i++)
for(int j=m;j>=a[i].q;j--)
f[j]=max(f[j],f[j-a[i].p]+a[i].w);
}
int main(int argc, const char * argv[]){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++){a[i].p=read();a[i].q=read();a[i].w=read();}
sort(a+,a++n);
dp();
printf("%d\n",f[m]);
}
return ;
}
HDU3466 Proud Merchants[背包DP 条件限制]的更多相关文章
- HDU3466 Proud Merchants [背包]
题目传送门 Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/O ...
- 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) ...
- 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) ...
- hdu 3466 Proud Merchants(有排序的01背包)
Proud Merchants Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) ...
随机推荐
- 【linux草鞋应用编程系列】_6_ 重定向和VT100编程
一.文件重定向 我们知道在linux shell 编程的时候,可以使用文件重定向功能,如下所示: [root@localhost pipe]# echo "hello world&q ...
- eclipse 突然 一直在loading descriptor for XXX (XXX为工程名)
问题: eclipse 启动后,啥也不干,就一直在loading descriptor for XXX (XXX为工程名),,其他什么操作都不能操作. 如下图所示,保存文件也无法保存. 这个怎么办? ...
- Idea创建Maven项目
- 从零开始学 Java - Windows 下安装 Eclipse
三观是什么鬼 当我们在讨论「三观一致」的时候是在讨论些什么? 我认为这个世界上本没有「三观」这一说法,说的人多了,也就有了「三观」这个词,当我们讨论「三观一致」其实并不是真的在说世界观.价值观.人生观 ...
- AdapterPattern(适配器模式)
import org.omg.PortableServer.AdapterActivator; /** * 分两种情况: * 1.类适配器 * 2.对象适配器 * 作用:让原本接口不兼容的两个类可以在 ...
- You Don't Need jQuery
前端发展很快,现代浏览器原生 API 已经足够好用.我们并不需要为了操作 DOM.Event 等再学习一下 jQuery 的 API.同时由于 React.Angular.Vue 等框架的流行,直接操 ...
- 网络分析之networkx(转载)
图的类型 Graph类是无向图的基类,无向图能有自己的属性或参数,不包含重边,允许有回路,节点可以是任何hash的python对象,节点和边可以保存key/value属性对.该类的构造函数为Graph ...
- GitHub学习心得之 分支操作
目录 前言 1. 一般的push和pull 2. 分支操作 前言 本文对Github的分支操作进行了总结, 主要基于以下文章: http://blog.csdn.net/guang11cheng/ar ...
- SQL Server 2008 R2 升级到 Service Pack 3后Report Builder启动不了
一同事将测试服务器从SQL Server 2008 R2 SP2升级到了SQL Server 2008 R2 SP3后发现Report Service的报表编辑时启动不了Report Builder, ...
- Ant:build.xml 结构
Ant build.xml 结构 project target task data property datatype v\:* {behavior:url(#default#VML);} o\:* ...