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

  1. 4 6
  2. 11 0 7 5 13 9
  3. 78 4 81 6 22 4
  4. 1 40 9 34 16 10
  5. 11 22 0 33 39 6
 

Sample Output

  1. 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]即为答案

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <algorithm>
  4. using namespace std;
  5. int const MAX = 2 * 1e5 + 5;
  6. int row[MAX], a[MAX], dp[MAX], tmp[MAX];
  7.  
  8. int main()
  9. {
  10. int n, m;
  11. while(scanf("%d %d", &n, &m) != EOF)
  12. {
  13. memset(tmp, 0, sizeof(tmp));
  14. memset(dp, 0, sizeof(dp));
  15. for(int i = 1; i <= m * n; i++)
  16. scanf("%d", &a[i]);
  17. int cnt = 1;
  18. for(int i = 1; i <= m * n; i += m)
  19. {
  20. for(int j = 2; j <= m; j++)
  21. {
  22. tmp[1] = a[i];
  23. tmp[j] = max(tmp[j - 1], a[i + j - 1] + tmp[j - 2]);
  24. }
  25. row[cnt ++] = tmp[m];
  26. }
  27. dp[1] = row[1];
  28. for(int i = 2; i < cnt; i++)
  29. dp[i] = max(dp[i - 1], dp[i - 2] + row[i]);
  30. printf("%d\n", dp[cnt - 1]);
  31. }
  32. }

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之更多的控制结构

    1.unless/if结构 unless 条件为假的时候 才执行语句块. eg: unless($fred =~ /^[A-Z_]\w*$/i){ print "The value of \ ...

  2. 【cookie】【浏览器】各大浏览器对cookie的限制

  3. Python 网络爬虫干货总结

    Python 网络爬虫干货总结 爬取 对于爬取来说,我们需要学会使用不同的方法来应对不同情景下的数据抓取任务. 爬取的目标绝大多数情况下要么是网页,要么是 App,所以这里就分为这两个大类别来进行了介 ...

  4. 剑指Offer(书):链表的倒数第K个节点

    题目:输入一个链表,输出该链表中倒数第k个结点. 分析:要注意三点:链表为空:链表个数小于k:k的值<=0; public ListNode FindKthToTail(ListNode hea ...

  5. Luogu3195 [HNOI2008]玩具装箱TOY (方程变形 + 斜率优化 )

    题意: 给出一个序列 {a[i]} 把其分成若干个区间,每个区间的价值为 W = (j − i + ∑ak(i<=k<=j) - L)​2 ,求所有分割方案中价值之和的最小值. 细节: 仔 ...

  6. 安装elk,日志采集系统

    #elasticsearch安装 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.0.0-linux ...

  7. python的re模块常用方法

    正则表达式模式 模式字符串使用特殊的语法来表示一个正则表达式: 字母和数字表示他们自身.一个正则表达式模式中的字母和数字匹配同样的字符串. 多数字母和数字前加一个反斜杠时会拥有不同的含义. 标点符号只 ...

  8. javascript异常cannot read property xx of null 的错误

    一般报这种异常或者错误,是因为试图从null中再读一个属性导致的. 比如:var myAttr=myObj.data.Name; 假如这个时候myObj.data是null,那么再试图读取data的N ...

  9. c++ 多线程:线程句柄可以提前关闭,但是线程并没有关闭

    很多程序在创建线程都这样写的:ThreadHandle = CreateThread(NULL,0,.....);CloseHandel(ThreadHandle );1,线程和线程句柄(Handle ...

  10. 【bzoj1710】[Usaco2007 Open]Cheappal 廉价回文

    [bzoj1710][Usaco2007 Open]Cheappal 廉价回文 Description 为了跟踪所有的牛,农夫JOHN在农场上装了一套自动系统. 他给了每一个头牛一个电子牌号 当牛走过 ...