HDU 2845 Beans (两次线性dp)
Beans
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3521 Accepted Submission(s): 1681
Problem Description
the qualities, but everyone must obey by the following rules: if you eat the bean at the coordinate(x, y), you can’t eat the beans anyway at the coordinates listed (if exiting): (x, y-1), (x, y+1), and the both rows whose abscissas are x-1 and x+1.
Now, how much qualities can you eat and then get ?
Input
the quality of bean isn't beyond 1000, and 1<=M*N<=200000.
Output
Sample Input
4 6
11 0 7 5 13 9
78 4 81 6 22 4
1 40 9 34 16 10
11 22 0 33 39 6
Sample Output
242
Source
Host by HDU
题目链接:
pid=2845">http://acm.hdu.edu.cn/showproblem.php?
pid=2845
题目大意:在一个矩阵中选择一些数,要求和最大,假设选择(x,y)位置的数。则(x, y+1),(x,y-1)位置不可选。第x+1和第x-1行都不可选
题目分析:题目给了m*n的范围,就是不让你开二维开开心心切掉。只是不影响。一维照样做。先对于每一行dp一下,求出当前行能取得的最大值
tmp[j] = max(tmp[j - 1],a[i + j - 1] + tmp[j - 2])第一个表示不选第i行第j列得数字。第二个表示选,取最大,则最后tmp[m]为当前行最大的
然后由于相邻两行不能同一时候取,我再对行做一次dp
dp[i] = max(dp[i - 1], dp[i - 2] + row[i]),第一个表示不选第i行,第二个表示选第i行,取最大,则最后dp[cnt - 1]即为答案
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 2 * 1e5 + 5;
int row[MAX], a[MAX], dp[MAX], tmp[MAX]; int main()
{
int n, m;
while(scanf("%d %d", &n, &m) != EOF)
{
memset(tmp, 0, sizeof(tmp));
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= m * n; i++)
scanf("%d", &a[i]);
int cnt = 1;
for(int i = 1; i <= m * n; i += m)
{
for(int j = 2; j <= m; j++)
{
tmp[1] = a[i];
tmp[j] = max(tmp[j - 1], a[i + j - 1] + tmp[j - 2]);
}
row[cnt ++] = tmp[m];
}
dp[1] = row[1];
for(int i = 2; i < cnt; i++)
dp[i] = max(dp[i - 1], dp[i - 2] + row[i]);
printf("%d\n", dp[cnt - 1]);
}
}
HDU 2845 Beans (两次线性dp)的更多相关文章
- HDU 2845 Beans (DP)
Beans Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- HDU 2845 Beans (DP)
Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...
- hdu 2845——Beans——————【dp】
Beans Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 2845 Beans(dp)
Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...
- HDU 2845 Beans (动态调节)
Beans Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- hdu 2845 Beans 2016-09-12 17:17 23人阅读 评论(0) 收藏
Beans Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- Hdu 2845 Beans
Beans Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 2845 Beans(最大不连续子序列和)
Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
随机推荐
- 序列化 random模块应用
序列化 我们今天学习下序列化,什么是序列化呢? 将原本的字典.列表等内容转换成一个字符串的过程就叫做序列化. 为什么要有序列化模块: 比如,我们在python代码中计算的一个数据需要给另外一段程序使用 ...
- awk中next以及getline用法示例
在awk中,如果调用next,那么next之后的命令就都不执行了.此行文本的处理到此结束,开始读取下一条记录并操作. 实例如下: [plain] view plain copy zoer@ubuntu ...
- openscad 3Dmodels 笔记
参考链接 官方文档 官方文档之--代码 如何快速上手 打开openSCAD后界面如下: 选择其中的examples,从basic看起.配合官方文档中的first step部分,和官方文档--代码写法即 ...
- bootshiro---开源的后台管理框架--基于springboot2+ shiro+jwt的真正rest api资源无状态认证权限管理框架,开发人员无需关注权限问题,后端开发完api,前端页面配置即可
https://gitee.com/tomsun28/bootshiro
- 如何使用百度地图API
一.申请密钥 1.先用eclipse创建一个Android工程 2.在百度api官网上申请一个密钥,链接:http://lbsyun.baidu.com/apiconsole/key 二.工程配置 1 ...
- LINQ-进行数据转换
一.将多个输入联接到一个输出序列中 可以使用 LINQ 查询创建包含元素的输出序列,这些元素来自多个输入序列. 以下示例演示如何组合两个内存中数据结构,但相同的原则可应用于组合来自 XML 或 SQL ...
- POJ-2318 TOYS,暴力+叉积判断!
TOYS 2页的提交记录终于搞明白了. 题意:一个盒子由n块挡板分成n+1块区 ...
- 【Go】错误处理
· error类型是一个接口类型,也是一个Go语言的内建类型.在这个接口类型的声明中只包含了一个方法Error.这个方法不接受任何参数,但是会返回一个string类型的结果.它的作用是返回错误信息的字 ...
- MHA 高可用集群搭建(二)
MHA 高可用集群搭建安装scp远程控制http://www.cnblogs.com/kevingrace/p/5662839.html yum install openssh-clients mys ...
- BZOJ 4824 [Cqoi2017]老C的键盘 ——树形DP
每一个限制条件相当于一条有向边, 忽略边的方向,就成了一道裸的树形DP题 同BZOJ3167 唯一的区别就是这个$O(n^3)$能过 #include <map> #include < ...