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里就不会爆内存了). (跑得很慢..大 ...
随机推荐
- 《HelloGitHub》第 19 期
前言 最近很少写博客了,工作上的事情太多(在做一些数据分析方面的工作,之前是 Web 开发),时间捉襟见肘.更多的时间都花在工作上,没有精力.时间积累整理知识.说来还是能力太差.效率有问题. 后面会好 ...
- 【问题排查】fastjson线上排坑记
前言 版本上线时发现fastjson的toString方法的返回的字符串与与之前版本的toString方法返回的字符串不相同,这导致依赖toString进行md5计算所得到的结果不相同,更进一步导致其 ...
- Go语言中slice使用注意事项
Go 语言中的slice类型可以理解为是数组array类型的描述符,包含了三个因素: 指向底层数组的指针 slice目前使用到的底层数组的元素个数,即长度 底层数组的最大长度,即容量 因此当我们定义一 ...
- angular指令的4种设计模式
指令的功能集非常丰富,不过我们已经发现了指令的帕累托分布:使用angular编写的大量指令只会用到可用性和设计模式中很小的比例,这些指令大概可以分为4类: 只渲染指令--这些指令将渲染作用域中的数据, ...
- Leetcode题解(十一)
31.Next Permutation 题目 这道题目的意思是给定一个序列,找出其按照字典顺序的下一个顺序,如果给定顺序是字典中的最后一个顺序,其下一个顺序则为字典中的第一个顺序.比如: 1,2,3, ...
- The Worm Turns
The Worm Turns Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- How Many Answers Are Wrong
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- replace 使用函数作为第二参数
var sToChange = “The sky is red.”;var reRed = /red/;var sResultText = sToChange.replace(reRed, funct ...
- RobotFramework自动化测试环境配置
现在工作是做自动化测试平台维护的,平台用的C#做的,主要是用来测试CMBRun项目,它是c/s结构的项目,而b/s结构的项目主要使用RF+Python来做.做这块之前听过自动化测试,身边的朋友也有做这 ...
- 几种移动app API调用认证方案浅析
最近做的金融项目,app调用的接口需要做一个身份认证,所以找了下目前API services验证的几种方式.之前翻译的一篇文章--[译]移动API安全终极指南中,主要提出了API服务调用验证的问题,通 ...