hdu2845 Beans--DP
发布日期:2021-10-03 20:31:54
浏览次数:4
分类:技术文章
本文共 1566 字,大约阅读时间需要 5 分钟。
原题链接:
一:原题内容
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 611 0 7 5 13 978 4 81 6 22 41 40 9 34 16 1011 22 0 33 39 6
Sample Output
242
二:分析理解
《压缩元素,借助递推》
首先,对于每一行,求出如果从这行挑元素(因为某个元素
被挑的话,它的上下行都不可以再被挑,而自身所在的那行还有
元素可以挑),可以得到的sum[i],这样“就把这行m个数压缩为
一个元素了”;
那么,对于n行都进行上面说的压缩(进行了n次),再竖着
这对着n个小sum进行一次同样的压缩,那么就得到了一个大SUM,
亦即ans。
分析转自:
三:AC代码
#include#include #include using namespace std;int a[200005];int b[200005];int main(){ int n, m, x; while (~scanf("%d%d", &n, &m)) { for (int i = 2; i <= n + 1; i++) { for (int j = 2; j <= m + 1; j++) { scanf("%d", &x); a[j] = max(a[j - 1], a[j - 2] + x); } b[i] = max(b[i - 1], b[i - 2] + a[m + 1]); } printf("%d\n", b[n + 1]); } return 0;}
转载地址:https://blog.csdn.net/LaoJiu_/article/details/50953091 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月24日 22时28分25秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
javascript解析json
2019-04-27
WinDbg安装与使用
2019-04-27
推荐阅读的多核编程技术书籍
2019-04-27
维基百科上的算法和数据结构链接很强大
2019-04-27
选择排序
2019-04-27
PHP session回收机制
2019-04-27
最新的全球编程语言,操作系统,web服务器等使用率分析报告
2019-04-27
用C语言写PHP扩展
2019-04-27
PHP Extension programming
2019-04-27
海量数据处理
2019-04-27
PHP防止注入攻击
2019-04-27
多路IO复用模型 select epoll 等
2019-04-27
Linux Epoll介绍和程序实例
2019-04-27
output_buffering详细介绍
2019-04-27
php缓冲 output_buffering和ob_start
2019-04-27
php error_reporting 详解
2019-04-27
剖析PHP中的输出缓冲
2019-04-27
HTTP响应头不缓存
2019-04-27
phpize
2019-04-27
PHP安装eAccelerator
2019-04-27