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. sqlServer2008技术内幕笔记总结(实用的sql方法总结)

    over函数的使用: 1.可以实现基于什么求和,省去group by: select xingming,xingbie,COUNT(*) over() as '总人数' from jbxx_xuesh ...

  2. vs2015+opencv3.3.1 实现 c++ 灰度高斯滤波器

    #include <opencv2\highgui\highgui.hpp> #include <iostream> #include<vector> using ...

  3. mac下redis和zookeeper启动及测试命令

    mac下启动命令:    sudo su - root cd /usr/local/bin/    ./redis-server ../etc/redis.conf cd /software/zook ...

  4. 初学python-字符串中引号的使用、input简介、强制类型转换、加减乘除简写、条件判断

    一.字符串中单引号和双引号的使用: 1.字符串以英文单引号' '引用.eg:'Hello World' 2.若字符串中含有单引号,则以英文双引号" "引用.eg:"I'm ...

  5. 从map中取出最大或最小value对应的key---多种写法

    package com.yuwanlong.hashing; import java.util.ArrayList; import java.util.Collections; import java ...

  6. Ping命令简单报错介绍

    了解ABC类IP地址:网络.主机.子网.广播. ---------------------------- 学会ping: ping www.baidu.com 网络检测:ping某一主机可以正常启动! ...

  7. Java网络编程客户端和服务器通信

    在java网络编程中,客户端和服务器的通信例子: 先来服务器监听的代码 package com.server; import java.io.IOException; import java.io.O ...

  8. c语言数据结构学习心得——队列

    队列 只允许在一端进行插入,在另一端进行删除的线性表 队头(Front):允许删除的一端(队首) 队尾(Rear):允许插入的一端 FIFO:先进先出 不要求从数组首位开始存储队列 #define M ...

  9. 在DZ 中 showmessage 中可以再次执行 JS

    showmessage ( '登录', '', array (), array (                         'showdialog' => 0,              ...

  10. Vue-think脚手架

    准备重构的项目,原来的后台是thinkPHP写的,刚刚摸VUE,不知道里面数据调用原理,想先安装vuethink学习一下. 结果安装半天,npm run dev的时候报错,尝试了很多方法,各种重装,看 ...