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

Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled with different qualities beans. Meantime, there is only one bean in any 1*1 grid. Now you want to eat the beans and collect
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

There are a few cases. In each case, there are two integer M (row number) and N (column number). The next M lines each contain N integers, representing the qualities of the beans. We can make sure that
the quality of bean isn't beyond 1000, and 1<=M*N<=200000.
 

Output

For each case, you just output the MAX qualities you can eat and then get.
 

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

2009 Multi-University Training Contest 4 -
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)的更多相关文章

  1. HDU 2845 Beans (DP)

    Beans Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  2. HDU 2845 Beans (DP)

    Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...

  3. hdu 2845——Beans——————【dp】

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

  4. HDU 2845 Beans(dp)

    Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...

  5. HDU 2845 Beans (动态调节)

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

  6. 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 ...

  7. Hdu 2845 Beans

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

  8. hdu 2845 Beans(最大不连续子序列和)

    Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...

  9. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

随机推荐

  1. perl学习之进程管理

    系统函数 == 最简单的系统调用  system "date"; # Perl会将 date 命令传递给unix的shell并获取返回值和error信息等   == 带有系统参数的 ...

  2. (转)Objective-C语言--属性和实例变量

    本文转自http://blog.csdn.net/addychen/article/details/39525681 使用Objective-C一段时间了,一直没有弄清楚在Objective-C中属性 ...

  3. Python模块--time&datetime

    一.Python中时间的表示方式 1.时间戳  如 1552623413.043036 2.格式化的时间字符串  如 2015-12-02 3.struct_time  是一个元组 共有九个元素 二. ...

  4. websphere8.5 与cxf2.x冲突问题

    一个客户was部署的小问题,记录一下. 问题现象 在我们的服务中用调用别人的webservice服务报错,框架用的cxf. 报错关键信息有: E com.ibm.ws.webcontainer.web ...

  5. Jmeter下载安装配置

    一,进入官网:http://jmeter.apache.org/ 1.第一步进入官网如下图 2.选择进行下载,下载下来为一个压缩包,解压即可. 3.我下载的是jmeter4.0版本,对应jdk1.8. ...

  6. numpy之flatnonzero函数

    Return indices that are non-zero in the flattened version of a. This is equivalent to a.ravel().nonz ...

  7. hdu_2070_Fibbonacci Number

    这个题我用long long ,printf("%lld");输出就错误了 我用__int64,printf("%I64d");输出就正确了 这点需要注意. # ...

  8. Nginx出现403 forbidden(Permission denied)报错的四种方法

    查看nginx的error.log日志.打开日志出现Permission denied: 1.启动用户和nginx的工作用户不一致所致 查看nginx的启动用户,发现是www,而为是用root启动的 ...

  9. PAT天梯赛练习题——L3-005. 垃圾箱分布(暴力SPFA)

    L3-005. 垃圾箱分布 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁 ...

  10. java面试题之Thread的run()和start()方法有什么区别

    run()方法: 是在主线程中执行方法,和调用普通方法一样:(按顺序执行,同步执行) start()方法: 是创建了新的线程,在新的线程中执行:(异步执行) public class App { pu ...