HDU5410--01背包+完全背包
CRB and His Birthday
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1655 Accepted Submission(s): 782
She went to the nearest shop with M Won(currency
unit).
At the shop, there are N kinds
of presents.
It costs Wi Won
to buy one present of i-th
kind. (So it costs k × Wi Won
to buy k of
them.)
But as the counter of the shop is her friend, the counter will give Ai × x + Bi candies
if she buys x(x>0)
presents of i-th
kind.
She wants to receive maximum candies. Your task is to help her.
1 ≤ T ≤
20
1 ≤ M ≤
2000
1 ≤ N ≤
1000
0 ≤ Ai, Bi ≤
2000
1 ≤ Wi ≤
2000
indicating the number of test cases. For each test case:
The first line contains two integers M and N.
Then N lines
follow, i-th
line contains three space separated integers Wi, Ai and Bi.
1
100 2
10 2 1
20 1 1
21HintCRB's mom buys 10 presents of first kind, and receives 2 × 10 + 1 = 21 candies.
题目大意:用m元去买商品,商品有n种,买商品的时候会附加赠送糖果,并且给出了各个商品需要的话费wi元,还有关于赠送糖果量的相关系数ai和bi。随着该物品的购买量x会赠送ai*x+bi个糖果。问,用m元如何购买商品能够获得的最多糖果数目。
解题思路:乍一看就是个完全背包,但是有额外增加的条件。随着购买量增加的赠送的糖果量。bi后面没有涉及到x。所以确定第一个的赠送量就是ai+bi。后面的赠送购买赠送的糖过量就和bi没有关系了,就变成了一个完全背包。刚开始的明显可以通过01背包来解决了。
源代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cstdlib>
#include<cmath>
#include<sstream>
#include<ctime>
using namespace std; int w[1005];
int dp[2005];
int a[1005];
int b[1005]; int main()
{
int t;
int m,n;//m钱款,n代表商品种类数
int i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
memset(w,0,sizeof(w));
memset(dp,0,sizeof(dp));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i = 0; i < n; i++)
{
scanf("%d%d%d",&w[i],&a[i],&b[i]);
}
for(i = 0; i < n; i++)
{
//先跑一次01背包,这样就不用再考虑bi,因为买与不买都会赠送bi糖果,
//0次或者1次考虑完之后,就剩下多次的,再跑完全背包
for(j = m; j >= w[i]; j--)
{
dp[j]=max(dp[j],dp[j-w[i]]+a[i]+b[i]);
}
for(j = w[i]; j <= m; j++)
{
dp[j]=max(dp[j],dp[j-w[i]]+a[i]);
}
}
printf("%d\n",dp[m]);
}
return 0;
}
HDU5410--01背包+完全背包的更多相关文章
- luogu 4377 Talent show 01分数规划+背包dp
01分数规划+背包dp 将分式下面的部分向右边挪过去,通过二分答案验证, 注意二分答案中如果验证的mid是int那么l=mid+1,r=mid-1,double类型中r=mid,l=mid; 背包dp ...
- 01二维背包——poj2576
/* 要求把a数组分成两个集合,两个集合人数最多差1,并且元素之和的差尽可能小 那只要把所有可行的列出来即可 01二维背包,即体积是个二维数据,那么我们的背包状态也应该设为二维 dp[j][k]设为 ...
- HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)
HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) pid=3591">http://acm.hdu.edu.cn/showproblem.php? ...
- 背包!背包!HDU 2602 Bone Collector + HDU 1114 Piggy-Bank + HDU 2191 512
http://acm.hdu.edu.cn/showproblem.php?pid=2602 第一题 01背包问题 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- dp--01背包,完全背包,多重背包
背包问题 以下代码 n是物品个数,m是背包容积 物品价值和重量int v[maxn],w[maxn]; 01背包 模板 for(int i = 0; i < n; i++) { for(int ...
- POJ 3260 多重背包+完全背包
前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168 ...
- 【poj3260-最少找零】多重背包+完全背包
多重背包+完全背包. 买家:多重背包:售货员:完全背包: 开两个数组,分别计算出买家,售货员每个面额的最少张数. 最重要的是上界的处理:上界为maxw*maxw+m(maxw最大面额的纸币). (网上 ...
- B 维背包+完全背包 Hdu2159
<span style="color:#3333ff;">/* ---------------------------------------------------- ...
- bzoj5281/luogu4377 Talent Show (01分数规划+背包dp)
就是01分数规划的思路,只不过当把w[i]-r*t[i]>0的选完以后如果w值还没达到要求,那就再01背包dp一下就好了(dp时w值>W的时候就存在W里就不会爆内存了). (跑得很慢..大 ...
随机推荐
- json格式转化成map
public static Map<String, Object> parseJSON2Map(String jsonStr) { Map<String, Object> ma ...
- web端/h5端账号密码的安全性问题
firefox一直提示让浏览器记住密码会有安全问题,但是一直未曾关注过到底是什么安全问题. 国庆节回家后发生的一件小事,让我深刻认识到让浏览器记住密码有多么不安全. 事情的起因是这样,家里wifi信号 ...
- git常用命令集合
git命令 git init:创建一个仓库并在目录下新建一个.git的文件(初始化一个git仓库) 注:.git文件在工作区,是一个隐藏文件(用ls -ah命令查看),但是它不算工作区,而是git 的 ...
- 手工搭建ABP框架(1) - Web项目
为了防止不提供原网址的转载,特在这里加上原文链接: http://www.cnblogs.com/skabyy/p/7295533.html ABP是 ASP.NET Boilerplate Proj ...
- 跨域请求CORS
参考:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS http://www.ruanyifeng.com/b ...
- body.clientHeight与documentElement.clientHeight
body上的clientHeight会对着内容区域的高度变化而变化,当内容只有100px,则body上只有100px被撑起,返回的clientHeight为100: documentElement.c ...
- sublime text全程指南【转载】
前言(Prologue) Sublime Text是一款跨平台代码编辑器(Code Editor),从最初的Sublime Text 1.0,到现在的Sublime Text 3.0,Sublime ...
- Spring IOC容器分析(1) -- BeanFactory
搭建好源码阅读环境后,就可以慢慢走进Spring殿堂了.IOC是Inversion of Control的缩写,控制反转的意思.很多人可能都知道IOC是spring的核心,将对象的创建初始化等权限交由 ...
- react-native多图选择、图片裁剪(支持ad/ios图片个数控制)
扯淡: 目前关于rn比较知名并且封装好的图片选择控件很多,不过能同时支持多图片上传,个数控制兼容iOS/Ad的却寥寥无几,而今天介绍的这款框架可以实现:图片裁剪.最大图片个数限制.拍照.本地相册等功能 ...
- 小程序 wx.getRecorderManager 录音 to 语音识别
微信扫小程序码看调用效果(自然语言理解小助手) 欢迎转载,请保留原文链接:http://www.happycxz.com/m/?p=125 这次主要是把我的api更新了一下,支持微信小程序新的录音接口 ...