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. 如何用纯 CSS 创作一个菱形 loader 动画

    效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/eKzjqK 可交互视频教 ...

  2. [译]The Python Tutorial#9. Classes

    写在前面 本篇文章是<The Python Tutorial>(3.6.1),第九章,类的译文. 9. Classes 与其他编程语言相比,Python的类机制定义类时,最小化了新的语法和 ...

  3. 查看zookeeper管理的solrcloud配置文件

    进入zookeeper/bin目录下 连接zookeeper sh zkCli.sh -server localhost:2181 查看 ls /configs 结果如下 [zk: localhost ...

  4. 组队赛Day1第一场 GYM 101350 G - Snake Rana (容斥)

    [题意] 给一个N×M的矩阵, K个地雷的坐标.求不含地雷的所有矩形的总数. T组数据. N M都是1e4,地雷数 K ≤ 20 Input 3 2 2 1 2 2 6 6 2 5 2 2 5 100 ...

  5. 排序算法C语言实现——堆排序

    /*堆排nlog(n)*//*堆排复杂度分析1.建堆((n*log(n))/2)    循环n/2次,每次调用HeapAdjust函数    HeapAdjust内部循环log(n)2.调整堆(((n ...

  6. 纯干货!live2d动画制作简述以及踩坑

    本文来自网易云社区,转载务必请注明出处. 1. 概述 live2d是由日本Cybernoids公司开发,通过扭曲像素位置营造伪3d空间感的二维动画软件.官网下载安装包直接安装可以得到两种软件,分别是C ...

  7. Python第三方库之openpyxl(11)

    Python第三方库之openpyxl(11) Stock Charts(股票图) 在工作表上按特定顺序排列的列或行中的数据可以在股票图表中绘制.正如其名称所暗示的,股票图表通常被用来说明股价的波动. ...

  8. javascript数组学习1

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. mysqlbinlog备份和mysqldump备份

    -bash : mysqldump: command not found -bash : mysqlbinlog:command not found 首先得知道mysql命令或mysqldump命令的 ...

  10. 【kmp或扩展kmp】HDU 6153 A Secret

    acm.hdu.edu.cn/showproblem.php?pid=6153 [题意] 给定字符串A和B,求B的所有后缀在A中出现次数与其长度的乘积之和 A和B的长度最大为1e6 方法一:扩展kmp ...