hdu2845 Beans--DP
发布日期:2021-10-03 20:31:54
浏览次数:3
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2024年03月28日 16时56分37秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
InnoDB与MyISAM的区别
2019-04-26
思科 Packet Tracer 实验六 RIP路由协议基本配置
2019-04-26
计算机网络实验七:DHCP基本配置
2019-04-26
计算机网络实验八:思科NAT的基本配置
2019-04-26
三郎数据结构算法学习笔记:单向环形链表约瑟夫问题
2019-04-26
前端特效H5+css+js:动态可拉进度条+附完整源码
2019-04-26
三郎数据结构学习笔记:双向循环链表(判断是否对称)附源码
2019-04-26
三郎数据结构算法学习笔记:基数排序
2019-04-26
三郎数据结构算法学习笔记:斐波那契(黄金分割法)查找算法
2019-04-26
Java中标识符的命名规则是什么?硬性要求和非硬性要求
2019-04-26
Java中八种基本数据类型的大小,以及他们的封装类
2019-04-26
Spring依赖注入的方式有几种,各是什么?
2019-04-26
SpringMVC怎么样设定重定向和转发的?
2019-04-26
SpringMVC常用的注解有哪些?
2019-04-26
spring bean的生命周期
2019-04-26
计算机网络子网划分详解
2019-04-26
计算机网络生成树算法STP简介
2019-04-26
三郎数据结构算法学习笔记:哈希表查找
2019-04-26
三郎数据结构算法学习笔记:二叉树的三种遍历及增删改查
2019-04-26
三郎数据结构算法学习笔记:顺序存储二叉树
2019-04-26