Problem Description
Today is CRB's birthday. His mom decided to buy many presents for her lovely son. 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
 
Input
There are multiple test cases. The first line of input contains an integer T, 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.
 
Output
For each test case, output the maximum candies she can gain.
 
Sample Input
1
100 2
10 2 1
20 1 1
 
Sample Output
21

Hint

CRB's mom buys 10 presents of first kind, and receives 2 × 10 + 1 = 21 candies.

 
Author
KUT(DPRK)
 
Source
 

题意:你有M块钱,现在有N件商品

第i件商品要Wi块,如果你购买x个这样的商品,你将得到Ai*x+Bi个糖果

问能得到的最多的糖果数

 

思路:非常好的一道01背包和完全背包结合的题目

首先,对于第i件商品,如果只买1个,得到的价值是Ai+Bi

如果在买1个的基础上再买,得到的价值就是Ai

也就是说,除了第一次是Ai+Bi,以后购买都是Ai

那么,我们能否将i商品拆分成两种商品,其中两种商品的代价都是Wi,

第一种的价值是Ai+Bi,但是只允许买一次

第二种的价值是Ai,可以无限次购买

 

接下来我们来讨论这样拆的正确性

理论上来讲,买第二种之前,必须要买第一种

但是对于这道题,由于Ai+Bi>=Ai是必然的,因为Bi肯定是非负

所以对于代价相同,价值大的肯定会被先考虑

换句话来说,如果已经开始考虑第二种商品了,那么第一种商品就肯定已经被添加到背包里了~

 

所以,这题我们把n件商品拆分成2*n件商品,对于第一种商品做01背包,对于第二种商品做完全背包,这样就把题目转换成了非常熟悉的题目,也就能顺利AC了

 
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define N 1006
int v,n;
int w[N<<],a[N<<],b[N<<];
int dp[N<<];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&v,&n);
for(int i=;i<=n;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
w[i]=x,a[i]=y+z;
w[i+n]=x,a[i+n]=y;
}
memset(dp,,sizeof(dp)); for(int i=;i<=n;i++)
{
for(int j=v;j>=w[i];j--)
{
dp[j]=max(dp[j],dp[j-w[i]]+a[i]);
}
} for(int i=n+;i<=*n;i++)
{
for(int j=w[i];j<=v;j++)
{
dp[j]=max(dp[j],dp[j-w[i]]+a[i]);
}
} printf("%d\n",dp[v]); }
return ;
}

hdu 5410 CRB and His Birthday(混合背包)的更多相关文章

  1. hdu 5410 CRB and His Birthday 01背包和全然背包

    #include<stdio.h> #include<string.h> #include<vector> #include<queue> #inclu ...

  2. HDU 5410 CRB and His Birthday (01背包,完全背包,混合)

    题意:有n种商品,每种商品中有a个糖果,如果买这种商品就送多b个糖果,只有第一次买的时候才送.现在有m元,最多能买多少糖果? 思路:第一次买一种商品时有送糖果,对这一次进行一次01背包,也就是只能买一 ...

  3. HDU 5410 CRB and His Birthday(完全背包变形)

    CRB and His Birthday Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  4. HDU 5410 CRB and His Birthday ——(完全背包变形)

    对于每个物品,如果购买,价值为A[i]*x+B[i]的背包问题. 先写了一发是WA的= =.代码如下: #include <stdio.h> #include <algorithm& ...

  5. HDU 5410 CRB and His Birthday

    题目大意: 一个人要去买礼物,有M元.有N种礼物,每件礼物的价值是Wi, 你第i件礼物买k个 是可以得到 Ai * k + Bi 个糖果的. 问怎么才能使得你得到的糖果数目最多.   其实就是完全背包 ...

  6. HDU 5410(2015多校10)-CRB and His Birthday(全然背包)

    题目地址:HDU 5410 题意:有M元钱,N种礼物,若第i种礼物买x件的话.会有Ai*x+Bi颗糖果,现给出每种礼物的单位价格.Ai值与Bi值.问最多能拿到多少颗糖果. 思路:全然背包问题. dp[ ...

  7. HDU 3535 分组混合背包

    http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意:有n组工作,T时间,每个工作组中有m个工作,改组分类是s,s是0是组内至少要做一件,是1时最多做一件 ...

  8. HDU 3535 AreYouBusy(混合背包)

    HDU3535 AreYouBusy(混合背包) http://acm.hdu.edu.cn/showproblem.php?pid=3535 题意: 给你n个工作集合,给你T的时间去做它们.给你m和 ...

  9. HDU 3535 AreYouBusy (混合背包)

    题意:给你n组物品和自己有的价值s,每组有l个物品和有一种类型: 0:此组中最少选择一个 1:此组中最多选择一个 2:此组随便选 每种物品有两个值:是需要价值ci,可获得乐趣gi 问在满足条件的情况下 ...

随机推荐

  1. (转)Linux下Apache 限速模块安装笔记

    参考文章:http://www.pcmag.com.cn/solution/net/story/200704/51003104.shtml 限线程:http://dominia.org/djao/li ...

  2. android后台截屏实现(3)--编译screencap

    修改好之后就要编译了,screencap的编译是要在源码环境中进行的. 将修改后的screencap.cpp文件替换源码中的原始文件,然后修改screencap的Android.mk文件,修改后的文件 ...

  3. ExtJS4.2学习(11)——高级组件之Grid

    大纲: 1.首先,搭建起来一个最基础的Grid组件: 2.其次,利用前边MVC架构将代码重构: 3.再者,介绍下Grid的一些特性. 一.搭建基础的Grid组件 在文章的开始,我们首先简单的搭建一个G ...

  4. Ubuntu安装配置Qt环境

    安装 QT4.8.6库+QT Creator 2.4.1 下载地址发布 QT4.8.6库  http://mirrors.hustunique.com/qt/official_releases/qt/ ...

  5. C# XML流操作简单实例

    这里我们先介绍操作XML文件的两个对象:XmlTextReader和XmlTextWriter打开和读取Xml文件使用到的对象就是XmlTextReader对象.下面的例子打开了与程序在同一路径下的一 ...

  6. [.NET]Repeater控件使用技巧

    1.控制Repeater表格中的按钮显隐 1.1 定义方法 public void Repeater1_ItemDataBinding(object sender, RepeaterItemEvent ...

  7. js函数知识点

    1.即使写成functon a()也是可以调用外面定义的变量的,写(a,b),我估计是为了降低耦合性 2.即使写成function a()也是可以用arguments[a]来默认写了(a,b) 3.在 ...

  8. 在.NET MVC下不用iframe实现局部加载html

    最近在做个后台系统,之前都是用iframe来实现加载内容,左侧菜单不刷新.但一直不喜欢这种方法,有许多弊端.今天自己在网上查找了一番后找到了比较好的替代方案: 一.利用html的锚点标记来实现无刷新页 ...

  9. JQuery日历插件My97DatePicker日期范围限制

    My97DatePicker是一个非常优秀的日历插件,不仅支持多种调用模式,还支持日期范围限制. 常规的调用比较简单,如下所示: 1 <input class="Wdate" ...

  10. Flask Jinjia2 与 react

    Jinjia2 这是flask使用的模板工具,利用render_template方法可以方便的将后端的数据传给前端. 但是如果要使用react呢. 我如果在jsx中直接使用{{}}是不能输出变量的. ...