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. mac下Apache添加限速模块mod_bw

    官方文档: Apache2 - Mod_bw v0.7 Author : Ivan Barrera A. (Bruce) HomePage : Http://Ivn.cl/apache & h ...

  2. macos10.8.5原版系统dmg转iso

    网上非常多资料说使用UltraISO打开macos10.8.5, 将InstallESD.dmg提取出来, 然后再用UltraISO打开InstallESD.dmg,点转换格式, 选择iso, 然后用 ...

  3. 使用OpenXml实现生成数据字典文档(beta)

    最近项目在走验收流程,之前没有仔细看SOW文档,发现需要补好多份文档,其中就有数据字典,项目组不愿意花时间太多的时间弄这些文档,也不希望以后还要重复劳动力,最终决定做一个工具,方便自己生成数据字典文档 ...

  4. 邮件发送 EMailHelper

    引用: using System; using System.Collections.Generic; using System.Linq; using System.Net; using Syste ...

  5. java下io文件切割合并功能

    package cn.stat.p1.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...

  6. hihoCoder挑战赛14 A,B,C题解

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 题目1 : 不等式 时间限制:10000ms 单点时限:1000ms 内存限制:2 ...

  7. 第8章BOM笔记

    第八章 BOM 一. Window 在浏览器中window有双重角色,他既是JavaScript访问浏览器窗口的一个借口,又是ECMAscript 规定的Global对象. 1.全局作用域 由于win ...

  8. C#必须掌握的系统类

    系统类  Type类,Object类,String类, Arrary类,Console类, Exception类,GC类, MarshalByRefObject类, Math类. DateTime结构 ...

  9. .net转php laraval框架学习系列(三)项目实战---Route&Controllers

    本章来学习laravel的路由 一个简单的路由列子 Route::get('/', function() { return 'Hello World'; }); 路由的写法和Node的风格很相似.上面 ...

  10. Activiti工作流学习-----基于5.19.0版本(5)

    五.与Spring集成 实际项目中一般都有Spring的身影,与Spring集成使得Activiti的实用性得到提高.activiti和Spring整合需要activiti-spring的jar在类路 ...