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) ...
随机推荐
- ASP.NET vNext总结:EntityFramework7
ASP.NET vNext总结:EntityFramework7 源码分享:http://www.jinhusns.com/Products/Download/?type=xcj 1.概述 关于EF7 ...
- UDS(ISO14229-2006) 汉译(No.2参考标准)
下列参考文件对本文件的系统是不可或缺的.注明日期的参考,仅关于对其引用的版本适用.未注明日期的,仅最新引用的文档(包括任何修改)适用. ISO 7498-1,信息技术——开放系统互联(OSI)——基本 ...
- git的诞生
Git的诞生 很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了. Linus虽然创建了Linux,但Linux的壮大是靠全世 ...
- 【Spring】浅析Spring框架的搭建
c目录结构: // contents structure [-] Spring是什么 搭建Spring框架 简单Demo 1,建立User类 2,建立Test类 3,建立ApplicationCont ...
- Struts2(1) —— 概述
1.Struts2框架介绍 Struts2框架是MVC流程框架,适合分层开发,框架应用实现不依赖于Servlet,使用大量的拦截器来处理用户请求,属于无侵入式的设计. 2.Struts2框架的流程原理 ...
- 解决jQuery多个版本,与其他js库冲突方法
jQuery多个版本或和其他js库冲突主要是常用的$符号的问题,这个问题 jquery早早就有给我们预留处理方法了,下面一起来看看解决办法. 1.同一页面jQuery多个版本或冲突解决方法. < ...
- js for循环中i++ 和 ++i有什么区别?
平时都是这样写的for循环, for(var i = 0; i < 20 ; i++){ .... } 但我看有的人这样写 for (var i = 0; i < 20 ; ++i) { ...
- sharepoint2013用场管理员进行文档库的爬网提示"没有权限,拒绝"的解决方法
爬网提示被拒绝,场管理员明明可以打开那个站点的,我初步怀疑是:环回请求(LoopbackRequest)导致的 解决方法就是修改环回问题.修改注册表 具体操作方法: http://www.c-shar ...
- elasticsearch GIS空间查询问题解决
在GIS行业的应用越来越广泛,GIS最常用根据区域进行空间数据查询 我定义了两个方法,一起来看一下: /** * geodistance filter * 一个过滤器来过滤基于一个特定的距离从 ...
- Mysql数据库的基本概念和架构
数据库 1.键:主键是表中的标志列.一个键可能由几列组成.可以使用键作为表格之间的引用. CustomerID是Customers表的主键,当它出现在其他表,例如Orders表中的时候就称它为外键. ...