Beans

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3418    Accepted Submission(s): 1629

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
 
 
解题思路:通过两次动态规划,分别对每行和列进行dp。对于每行状态定义:dp[j]表示该行到达第j列时的最大的和。转移方程:dp[j]=max(dp[j-2]+a[j],dp[j-1])。然后用另外的一个d数组记录每行的最大和,相当于矩阵变成了一个列矩阵。再定义状态dq[i]为表示到达该行时的最大和。转移方程:dq[i]=max(dq[i-2]+d[i],dq[i-1])。
 
 
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn=211000;
int a[maxn];
int dp[maxn],d[maxn],dq[maxn];
int mmax(int a,int b,int c){
int ret;
ret=a>b?a:b;
return ret>c?ret:c;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
int i,j,k;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%d",&a[j]);
}
dp[0]=0,dp[1]=a[1];
for(j=2;j<=m;j++){
dp[j]=max(dp[j-2]+a[j],dp[j-1]);
}
d[i]=dp[m];
}
dq[1]=d[1];
for(i=2;i<=n;i++){
dq[i]=max(dq[i-1],dq[i-2]+d[i]);
}
printf("%d\n",dq[n]);
}
return 0;
}

  

hdu 2845——Beans——————【dp】的更多相关文章

  1. HDU 2845 Beans (DP)

    Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...

  2. HDU - 1260 Tickets 【DP】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1260 题意 有N个人来买电影票 因为售票机的限制 可以同时 卖一张票 也可以同时卖两张 卖两张的话 两 ...

  3. HDU 2845 Beans(dp)

    Problem Description Bean-eating is an interesting game, everyone owns an M*N matrix, which is filled ...

  4. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  5. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  6. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  7. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  8. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  9. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

随机推荐

  1. C语言编程学习:链表的来源分析

    C语言是面向过程的,而C++是面向对象的 C和C++的区别: C是一个结构化语言,它的重点在于算法和数据结构.C程序的设计首要考虑的是如何通过一个过程,对输入(或环境条件)进行运算处理得到输出(或实现 ...

  2. EF 完成指定数据迁移

    Update-Database -ConfigurationTypeName Bmw.Services.DealerService.WebApiHost.MigrationsOfDealerDB.De ...

  3. Castle Windsor 注册组件

    1.逐个注册组件即对每个接口通过代码指定其实现类,代码: container.Register( Component.For<IMyService>() //接口 .Implemented ...

  4. .net core 结合nlog使用Elasticsearch , Logstash, Kibana

    什么是ELK ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件.新增了一个FileBeat,它是一个轻量级的日志收集处理工具 ...

  5. 【大数据之数据仓库】HAWQ versus GreenPlum

    谈到GreenPlum,肯定会有同事说HAWQ!是的,在本系列第一篇选型流水记里,也有提到.因为对HAWQ接触有限,没有深入具体了解,所以很多信息都是来自于博文,人云亦云,我把看过的资料简要整理,希望 ...

  6. SiriShortCut模型建立及数据交互逻辑

    1.模型数据需求 意图: 手机号 密码 网关ID 打开该情景的命令 情景号 情景名 情景背景图 添加该意图时的 token值 主程序登陆共享数据 手机号 token值 2.操作逻辑 1.意图被唤起 获 ...

  7. python之freshman00

    编译型vs解释型 编译型优点:编译器一般会有预编译的过程对代码进行优化.因为编译只做一次,运行时不需要编译,所以编译型语言的程序执行效率高.可以脱离语言环境独立运行.缺点:编译之后如果需要修改就需要整 ...

  8. Vue 使用 axios post请求后台数据时 404

    今天遇到Vue 使用 axios post请求后台数据时 404 使用postman 就能获取到 网上找了大半天 终于找到了解决方法,传送门:https://www.jianshu.com/p/b10 ...

  9. [转][Java]使用Spring配合Junit进行单元测试的总结

    http://www.51testing.com/html/14/n-1408814.html 1.直接对spring中注入的bean进行测试(以DAO为例): 在测试类上添加@RunWith注解指定 ...

  10. QQ在线状态的使用

    在网页中显示QQ在线状态并点击后发起对话,是很多门户网站常见的一个功能,这两天就碰到这样一个.原以为很简单,结果还是折腾了半天,虽然是个小问题,但也值得记录一下. 按以前的经验,网上有很多QQ在线代码 ...