ACM学习历程—HDU5410 CRB and His Birthday(动态规划)
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.
题目大意像一个背包问题,物品可以放无穷次,但是增益是Ai×次数+Bi。
关键是多了B,不然就是一个普通的背包,能处理掉B,这题就能做。
于是考虑了这样一个状态p[flag][j]:
flag为真表示放过第i个物品的最优,flag为假表示不放第i个物品的最优。
j就是背包容量。
于是p[1][j] = max(p[0][j-w[i]]+a[i]+b[i], p[1][j-w[i]]+a[i]);
然后到i+1时,初始状态是都没有放过i+1物品的,所以需要初始化p[0][j]:
p[0][i] = max(p[0][i], p[1][i]);
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#define LL long long using namespace std; const int maxN = ;
int n, m, p[][];
int w[maxN], a[maxN], b[maxN]; void input()
{
scanf("%d%d", &m, &n);
for (int i = ; i < n; ++i)
scanf("%d%d%d", &w[i], &a[i], &b[i]);
memset(p, , sizeof(p));
} void work()
{
for (int i = ; i < n; ++i)
{
for (int j = w[i]; j <= m; ++j)
p[][j] = max(p[][j-w[i]]+a[i]+b[i], p[][j-w[i]]+a[i]);
for (int j = ; j <= m; ++j)
p[][j] = max(p[][j], p[][j]);
}
int ans = ;
for (int i = ; i <= m; ++i)
{
ans = max(ans, p[][i]);
ans = max(ans, p[][i]);
}
printf("%d\n", ans);
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
input();
work();
}
return ;
}
ACM学习历程—HDU5410 CRB and His Birthday(动态规划)的更多相关文章
- ACM学习历程—HDU5407 CRB and Candies(数论)
Problem Description CRB has N different candies. He is going to eat K candies.He wonders how many co ...
- ACM学习历程—HDU 5534 Partial Tree(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...
- ACM学习历程—Hihocoder 1290 Demo Day(动态规划)
http://hihocoder.com/problemset/problem/1290 这题是这次微软笔试的第三题,过的人比第一题少一点,这题一眼看过去就是动态规划,不过转移方程貌似不是很简单,调试 ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- ACM学习历程—HDU5521 Meeting(图论)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...
- ACM学习历程—HDU2476 String painter(动态规划)
http://acm.hdu.edu.cn/showproblem.php?pid=2476 题目大意是给定一个起始串和一个目标串,然后每次可以将某一段区间染成一种字符,问从起始串到目标串最少需要染多 ...
- ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)
http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...
- ACM学习历程—HDU5701 中位数计数(中位数 && 计数排序)
http://acm.hdu.edu.cn/showproblem.php?pid=5701 这是这次百度之星初赛2B的第六题.之前白山云做过类似的题,省赛完回来,我看了一下大概就有这样的思路:首先枚 ...
随机推荐
- client交互技术简单介绍
随着网络应用的不断丰富,client交互技术也如雨后春笋一般,遍地开花. 正是这些技术的支持,我们的互联网世界变得更加丰富多彩.一个浏览器上.不用说是简单的动画效果,就是一个Office应用也能顺畅的 ...
- Linux - 配置SSH免密通信 - “ssh-keygen”的基本用法
目录 1 什么是SSH 2 配置SSH免密登录 2.1 安装必需的软件 2.2 ssh-keygen创建公钥-私钥对 2.3 ssh-copy-id把A的公钥发送给B 2.4 在A服务器上免密登录B服 ...
- wifi认证Portal开发系列(二):FreeRadius的安装和测试、关联Mysql
注:本次安装是基于FreeRadius 3版本进行安装配置的,在配置Mysql的过程中,与2版本有些不同.操作系统是CentOS 7 一.准备工作 工具的安装 #安装rz.sz命令用于文件上传 yum ...
- 解决ListView滑动时出现黑边的问题
[声明]转载请注明出处,此文出自指尖飞落的博客:http://blog.csdn.net/huntersnail --尊重作者,知识无价.交流无限! 两种方法 1.代码去边缘线 myList.setF ...
- mapreduce代码实现入门
mapreduce代码主要包括三个类,map类.reduce类以及测试类! 以wordcount为例, map类为: static class WordMapper extends Mapper< ...
- CPI
CPI (Consumer Price Index 物价指数) 是政府用来衡量通货膨胀的其中一个数据.通俗的讲,CPI就是市场上的货物价格增长百分比.一般市场经济国家认为CPI在2-3%属于可接受范围 ...
- 多媒体开发之sps---解析sps得到图像的宽高
(1)通过块的宽高解析出真个h264的分辨率 如何解析SDP中包含的H.264的SPS和PPS串 http://www.pernet.tv.sixxs.org/thread-109-1-1.html ...
- 几句话搞懂URI、URL、URN之间的关系
1.URI,是uniform resource identifier,统一资源标识符,用来唯一的标识一个资源. 2.RL是uniform resource locator,统一资源定位器,它是一种具体 ...
- Louvain Modularity Fast unfolding of communities in large networks
Louvain Modularity Fast unfolding of communities in large networks https://arxiv.org/pdf/0803.0476.p ...
- Grunt学习笔记【3】---- filter使用方式和实现原理详解
本文主要讲配置任务中的filter,包括使用默认fs.Stats方法名和自定义过滤函数,以及filter的实现原理. 通过设置filter属性可以实现一些特殊处理逻辑.例如:要清理某个文件夹下的所有空 ...