CRB and His Birthday

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 430    Accepted Submission(s): 237

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
 
Recommend
wange2014   |   We have carefully selected several similar problems for you:  5416 5415 5414 5413 5412
 
大致三种方法:
1.  01背包+完全背包
先跑一遍01背包,价值为a+b的,然后再按价值为a的完全背包跑。

 #include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 2005
#define M 12 int n,m;
int f[N],w[N],a[N],b[N]; int main()
{
int T;cin>>T;
while(T--)
{
scanf("%d%d",&m,&n);
for(int i=;i<=n;i++)
scanf("%d%d%d",&w[i],&a[i],&b[i]); memset(f,,sizeof(f)); for(int i=;i<=n;i++)
{
for(int j=m;j>=w[i];j--)
{
f[j]=max(f[j],f[j-w[i]]+a[i]+b[i]);
}
for(int j=w[i];j<=m;j++)
{
f[j]=max(f[j],f[j-w[i]]+a[i]);
} }
cout<<f[m]<<endl;
}
return ;
}

2. 完全背包的思路。每次有三种选择方案,1不选第i件物品,2选一个第2件物品,3选2个以上的第二件物品。

用一个辅助数组g记录上一行(上一个i)的最大糖果数的值,因为完全背包空间是从小到大的,所以同一个i后面的空间会用到前面的的值,这也就是完全背包的内涵,但是用了辅助数组g,记录上一行的状态,就保证使用a+b不会用到前面的值。

转移方程:f[j]=max3(f[j], f[j-w[i]]+a[i], g[j-w[i]]+a[i]+b[i]);

 #include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 2005
#define M 12 int n,m;
int w[N],a[N],b[N];
int f[N];//f[j]表示j元钱可以得到的最大糖果数
int g[N];//g[j]表示上一个i,j元可以得到的最大糖果数 int max3(int a,int b,int c)
{
return max(a,max(b,c));
} int main()
{
int T;cin>>T;
while(T--)
{
scanf("%d%d",&m,&n);
for(int i=;i<=n;i++)
scanf("%d%d%d",&w[i],&a[i],&b[i]); memset(f,,sizeof(f));
memset(g,,sizeof(g)); for(int i=;i<=n;i++)
{
for(int j=w[i];j<=m;j++)
{
f[j]=max3(f[j], f[j-w[i]]+a[i], g[j-w[i]]+a[i]+b[i]);
}
for(int j=;j<=m;j++)
{
g[j]=f[j];
}
}
cout<<f[m]<<endl;
}
return ;
}

3. 还是完全背包的思路,用dp[i][j][0]表示不取第i件物品,花费j得到的最大收益,dp[i][j][1]表示取第i件物品,花费j得到的最大收益,最终的结果就是max(dp[i][j][0],dp[i][j][1])。每次对于物品i,先得出不取它能得到的最大收益,然后再求取它能得到的最大收益。

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

第一位空间可以压缩,把声明改成 int dp[N][2];最后结果改成max(dp[m][0],dp[m][1]).

中间关键代码换成

    for ( int j = ; j <= m; j++ )
{
dp[i][j][] = max( dp[i-][j][], dp[i-][j][] );
}
for ( int j = w; j <= m; j++ )
{
dp[i][j][] = max( dp[i][j - w][] + a, dp[i][j - w][] + a + b );
}

HDU 5410 CRB and His Birthday(完全背包变形)的更多相关文章

  1. hdu 5410 CRB and His Birthday(混合背包)

    Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son ...

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

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

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

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

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

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

  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 2159 FATE(二维费用背包)

    FATE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  8. HDU 1712 ACboy needs your help(包背包)

    HDU 1712 ACboy needs your help(包背包) pid=1712">http://acm.hdu.edu.cn/showproblem.php? pid=171 ...

  9. HDU 2639 Bone Collector II(01背包变形【第K大最优解】)

    Bone Collector II Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

随机推荐

  1. Node.js高级编程读书笔记 - 3 网络编程

    Outline 3.4 构建TCP服务器 3.5 构建HTTP服务器 3.6 构建TCP客户端 3.7 创建HTTP请求 3.8 使用UDP 3.9 用TLS/SSL保证服务器的安全性 3.10 用H ...

  2. 腾讯优测-优社区干货精选 |  那些年,我们在Android机型适配上遇到的坑之Camera拍照时快门咔嚓声

    文/腾讯优测研发工程师 吴宇焕 优测小优有话说: android机型适配的坑自然是不少,不想掉坑快来优测优社区~ 现在Android手机一般都会带有照相功能,有很多朋友就发现手机照相时快门声音很响,想 ...

  3. ppDelegate的相关介绍

    //  AppDelegate的相关介绍//  IOS笔记 //@interface AppDelegate : UIResponder <UIApplicationDelegate>// ...

  4. MongoDB学习笔记四:索引

    索引就是用来加速查询的.创建数据库索引就像确定如何组织书的索引一样.但是你的优势是知道今后做何种查询,以及哪些内容需要快速查找.比如:所有的查询都包括"date"键,那么很可能(至 ...

  5. express创建项目

    sudo apt-get install node-express-generator dave@voctrals:~/WebstormProjects/nodejs-study/express$ e ...

  6. 基于Open vSwitch搭建虚拟路由器

    As part of my work in OpenDaylight, we are looking at creating a router using Open vSwitch... Why? W ...

  7. vim全选,全部复制,全部删除

    全选(高亮显示):按esc后,然后ggvG或者ggVG 全部复制:按esc后,然后ggyG 全部删除:按esc后,然后dG 解析: gg:是让光标移到首行,在vim才有效,vi中无效 v : 是进入V ...

  8. centos 7 下安装Nginx

    下载Nginx wget nginx.tar.gz http://nginx.org/download/nginx-1.11.3.tar.gz 解压源码 .tar.gz 然后进入目录编译安装 cd n ...

  9. 一、Owin Identity的使用

    参照http://www.cnblogs.com/r01cn/p/5177708.html教程. 注意点: 1.Nuget包分别下载Microsoft.AspNet.Identity(Owin必须), ...

  10. 【解决】如何导入导出SharePoint列表和文档库

    早期的SharePoint管理工具stsadm.exe只能导出/导入网站,但不能导出/导入列表和文档库.但在PowerShell增加了此命令,具体操作如下. I. 导出列表或文档库 Export-SP ...