Beans

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
 思路:
STEP_1:先以行为单位来算,设DP数组存的是以此位置为起点能得到的最大值,在此前提下,DP[i][j]因为相邻的不能走,所以要么加上DP[i][j + 2],要么加上DP[i][j + 3] (假设不越界),取最大的那个就好。从右往左依次计算这一行每个元素的DP值,记录下最大的,放到DP[i][0]里。
STEP_2:重复第一步,只不过对象换成了DP数组里每行0号单元的值,因为此单元放的是此行内的最大值。从上往下,每次选定一行后设此行为最终答案里最下面那行,因为选定一行之后它的上面和下面那一行就废掉了,所以每一行要么加上它上面第二行,要么加上它上面第三行。同理,记录下最大值,最大值即答案。(实际上答案不是最后一行就是倒数第二行,因为没有负数,只会越加越大,不过只有1行的时候就会越界,虽然HDU上的数据貌似没1行的)。
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 200105 int main(void)
{
int n,m;
int max,temp_1,temp_2,ans; while(scanf("%d%d",&n,&m) != EOF)
{
int dp[n + ][m + ]; memset(dp,,sizeof(dp));
for(int i = ;i <= n;i ++)
for(int j = ;j <= m;j ++)
scanf("%d",&dp[i][j]); for(int i = ;i <= n;i ++)
{
max = dp[i][m];
for(int j = m;j >= ;j --)
{
dp[i][j] += dp[i][j + ] > dp[i][j + ] ? dp[i][j + ] : dp[i][j + ];
max = max > dp[i][j] ? max : dp[i][j];
}
dp[i][] = max;
} max = dp[][];
dp[][] += dp[][];
for(int i = ;i <= n;i ++)
{
dp[i][] += dp[i - ][] > dp[i - ][] ? dp[i - ][] : dp[i - ][];
max = max > dp[i][] ? max : dp[i][];
} printf("%d\n",max);
} return ;
}
 

HDU 2845 Beans (DP)的更多相关文章

  1. HDU 2845 Beans (两次线性dp)

    Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  2. HDU 2845 Beans (DP)

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

  3. hdu 2845——Beans——————【dp】

    Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  4. HDU 2845 Beans(dp)

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

  5. HDU 2845 Beans (动态调节)

    Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  6. Hdu 2845 Beans

    Beans Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. 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 ...

  8. hdu 2845 Beans(最大不连续子序列和)

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

  9. hdu 2845简单dp

    /*递推公式dp[i]=MAX(dp[i-1],dp[i-2]+a[j])*/ #include<stdio.h> #include<string.h> #define N 2 ...

随机推荐

  1. (剑指Offer)面试题32:从1到n整数中1出现的次数

    题目: 输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从1到12这些整数中包含1的数字有1,10,11和12,一共出现了5次. 思路: 1.累加法 累加1到n中每个整数 ...

  2. spring含参数 环绕通知demo

    题目:有一个懂得读心术的人需要完成两件事情:截听志愿者的内心感应和显示他们在想什么 <?xml version="1.0" encoding="UTF-8" ...

  3. Thinkphp的Volist标签

    Volist标签主要用于在模板中循环输出数据集或者多维数组. volist标签(循环输出数据) 闭合 非闭合标签 属性 name(必须):要输出的数据模板变量 id(必须):循环变量 offset(可 ...

  4. 你应该知道的JavaScript中NaN的秘密

    NaN,不是一个数字,是一种特殊的值来代表不可表示的值,使用typeof或其他任何与之比较的处理方式,‘NaN’则会引起一些混乱, 一些操作会导致NaN值的产生.这里有些例子: Math.sqrt(- ...

  5. cdoj 24 8球胜负(eight) 水题

    8球胜负(eight) Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/24 ...

  6. 几种Java写webservice的比较

    Java6,Axis2.XFire.CXF 1.JWS是Java语言对WebService服务的一种实现,用来开发和发布服务.而从服务本身的角度来看JWS服务是没有语言界限的.但是Java语言为Jav ...

  7. 如何进行js动态生成option?如何实现二级连动?

    何为二级连动? 首先要明白什么是二级连动!顾名思义,就是一个动,另外一个也跟着一起动 看下面的例子: 这里有一个“市级”的选择列表框,还有一个“县级”的选择列表框,如果“市级”的选择列表框中的值发现变 ...

  8. jQuery 效果 - animate() 方法

    http://www.w3school.com.cn/jquery/effect_animate.asp 实例 改变 "div" 元素的高度: $(".btn1" ...

  9. nginx 流媒体 flv 播放 以及上传大小 配置文件设置

    nginx 流媒体 flv 播放 以及上传大小 配置文件设置   server {listen 80;server_name localhost;root /www/web/default;index ...

  10. Javascript加载执行问题探索

    转自:http://www.cnblogs.com/huangxincheng/archive/2011/12/04/2275988.html 前言 最近研究MongoDB数据库,无意间发现的好博客, ...