HDU 2845 Beans (两次线性dp)
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
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
the quality of bean isn't beyond 1000, and 1<=M*N<=200000.
Output
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
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)的更多相关文章
- HDU 2845 Beans (DP)
Beans Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- HDU 2845 Beans (DP)
Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...
- hdu 2845——Beans——————【dp】
Beans Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 2845 Beans(dp)
Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...
- HDU 2845 Beans (动态调节)
Beans Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 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 ...
- Hdu 2845 Beans
Beans Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- hdu 2845 Beans(最大不连续子序列和)
Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
随机推荐
- perl之更多的控制结构
1.unless/if结构 unless 条件为假的时候 才执行语句块. eg: unless($fred =~ /^[A-Z_]\w*$/i){ print "The value of \ ...
- 【cookie】【浏览器】各大浏览器对cookie的限制
- Python 网络爬虫干货总结
Python 网络爬虫干货总结 爬取 对于爬取来说,我们需要学会使用不同的方法来应对不同情景下的数据抓取任务. 爬取的目标绝大多数情况下要么是网页,要么是 App,所以这里就分为这两个大类别来进行了介 ...
- 剑指Offer(书):链表的倒数第K个节点
题目:输入一个链表,输出该链表中倒数第k个结点. 分析:要注意三点:链表为空:链表个数小于k:k的值<=0; public ListNode FindKthToTail(ListNode hea ...
- Luogu3195 [HNOI2008]玩具装箱TOY (方程变形 + 斜率优化 )
题意: 给出一个序列 {a[i]} 把其分成若干个区间,每个区间的价值为 W = (j − i + ∑ak(i<=k<=j) - L)2 ,求所有分割方案中价值之和的最小值. 细节: 仔 ...
- 安装elk,日志采集系统
#elasticsearch安装 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.0.0-linux ...
- python的re模块常用方法
正则表达式模式 模式字符串使用特殊的语法来表示一个正则表达式: 字母和数字表示他们自身.一个正则表达式模式中的字母和数字匹配同样的字符串. 多数字母和数字前加一个反斜杠时会拥有不同的含义. 标点符号只 ...
- javascript异常cannot read property xx of null 的错误
一般报这种异常或者错误,是因为试图从null中再读一个属性导致的. 比如:var myAttr=myObj.data.Name; 假如这个时候myObj.data是null,那么再试图读取data的N ...
- c++ 多线程:线程句柄可以提前关闭,但是线程并没有关闭
很多程序在创建线程都这样写的:ThreadHandle = CreateThread(NULL,0,.....);CloseHandel(ThreadHandle );1,线程和线程句柄(Handle ...
- 【bzoj1710】[Usaco2007 Open]Cheappal 廉价回文
[bzoj1710][Usaco2007 Open]Cheappal 廉价回文 Description 为了跟踪所有的牛,农夫JOHN在农场上装了一套自动系统. 他给了每一个头牛一个电子牌号 当牛走过 ...