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. 美丽的for循环语句

    美丽的for循环语句 题目:用for循环语句实现四个三角形不同的形状.   图案:    ---------------第一个三角形图形形状----------------**********第二个三 ...

  2. 修改Android中strings.xml文件, 动态改变数据

    有些朋友可能会动态的修改Android中strings.xml文件中的值,在这里给大家推荐一种简单的方法.strings.xml中节点是支持占位符的,如下所示: <string name=&qu ...

  3. Android的Recovery中font_10x10.h字库文件制作

    任务是要汉化Android中的Recovery,就了解了bootable/recovery/minui/font_10x18.h这个英文字库的来历,最终汉化的时候并没有自己汉字字库,用的github上 ...

  4. springMVC 注解版

    http://blog.csdn.net/liuxiit/article/details/5756115 http://blog.csdn.net/hantiannan/article/categor ...

  5. 【ThinkingInC++】64、重载new和delete,来模仿内存的分配

    /** * 书本:[ThinkingInC++] * 功能:重载new和delete.来模仿内存的分配 * 时间:2014年10月5日14:30:11 * 作者:cutter_point */ #in ...

  6. [小技巧][ASP.Net MVC Hack] 使用 HTTP 报文中的 Header 字段进行身份验证

    在一些 Web 系统中,身份验证是依靠硬件证书进行的:在电脑上插入 USB 证书,浏览器插件读取证书的相关信息,然后在发送 HTTP 登录请求时顺便在 Header 字段附加上身份信息.服务器端处理这 ...

  7. NPM与调试工具的使用

    1)NPM 2)nodemon 自动监视文件的变化并重启应用 3)pm2 启动node,资源共享 4)node-inspector node调试工具 5)Chrome Developer Tools

  8. css部分总结

    10.19HTML总结 1.<!DOCTYPE HTML>声明:告知浏览器文档使用哪种HTML或者XHTML规范,该标签可声明三种DTD(文档类型定义)类型:严格版本.过渡版本以及基于框架 ...

  9. JCo 指南

    http://blog.csdn.net/asdfak/article/details/5834731 JAVA 调用SAP端接口 Java Connector and BAPI 前些日子想去深入的研 ...

  10. ORACLE基本SQL语句-查询篇

    一.普通查询 /*查询表数据*/select * from STU /*取出前3行数据*/select * from stu where ROWNUM<=3 /*模糊查询*/select * f ...