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. Routed Events【pluralsight】

    Routing Strategies: Direct Bubbling Tunneling WHy use them? Any UIElement can be a listener Common h ...

  2. Tomcat设置自己的项目为默认项目(用IP访问的是自己的项目)

    方法一:将项目拷贝到webapps下,并更名为ROOT; 方法二:在tomcat/conf/service.xml的<host></host>中配置 <Context p ...

  3. application与cache

    每个项目都有一些全局,常用的信息,而这些信息如果在每次使用时都载入,那必将耗费很大的资源,特别是对访问压力大的系统.因此,这个情况中,把这些全局信息放到缓存中是很必要的,放在缓存中可以使得数据能够很快 ...

  4. ags js下载地址

    https://developers.arcgis.com/en/downloads/ 备用

  5. Spring aop expression

    任意公共方法的执行:execution(public * *(..))任何一个名字以“set”开始的方法的执行:execution(* set*(..))AccountService接口定义的任意方法 ...

  6. erlang 查看进程相关信息

    出自: http://blog.sina.com.cn/s/blog_96b8a1540100zczz.html

  7. Javascript(JS)对Cookie的读取、删除、写入操作帮助方法

    var CookieUtils = { get: function (name) { var cookieName = encodeURIComponent(name) + '=', cookieSt ...

  8. IAR EWARM Argument variables $PROJ_DIR$ $TOOLKIT_DIR$

    在IAR中的help中输入argument variables时会找到这样的一个列表: Argument variables On many of the pages in the Options d ...

  9. Codeforces Round #140 (Div. 1) D. The table 构造

    D. The table 题目连接: http://www.codeforces.com/contest/226/problem/D Description Harry Potter has a di ...

  10. Codeforces Gym 100231G Voracious Steve 记忆化搜索

    Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...