Beans

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4456    Accepted Submission(s): 2105

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
 

题目的意思是在一个矩阵中取数字,取数规则是如果取了一个数a[i][j],那么他的上一行和下一行和前后两个数都不能取了。

处理时先在每一行算出最大不连续子序列的和,保存到一个数组里,当所有行都处理完之后,我们发现在列上也是一个球最大不连续子序列和的问题

由于数据较大,数组开不下,我们采用输一行处理一行

对于每个数dp[i]如果取它的话最大值一定是他到前面2个或3个的最大值加上他自己,即dp[i]=max(dp[i-2],dp[i-3])+a[i];因为i-1不能取,而i-4升至更大的话,中间空了一个可以取得数。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f int dp[200005];
int b[200005];
int a[200005];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
{
scanf("%d",&a[j]);
} dp[0]=a[0];
dp[1]=a[1];
dp[2]=dp[0]+a[2];
for(int j=3;j<m;j++)
{
dp[j]=max(dp[j-2],dp[j-3])+a[j];
}
b[i]=max(dp[m-1],dp[m-2]);
}
dp[0]=b[0];
dp[1]=b[1];
dp[2]=dp[0]+b[2];
for(int j=3;j<n;j++)
{
dp[j]=max(dp[j-2],dp[j-3])+b[j];
}
int ans=max(dp[n-1],dp[n-2]);
printf("%d\n",ans); } return 0;
}


hdu 2845 Beans 2016-09-12 17:17 23人阅读 评论(0) 收藏的更多相关文章

  1. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  2. hdu 1232, disjoint set, linked list vs. rooted tree, a minor but substantial optimization for path c 分类: hdoj 2015-07-16 17:13 116人阅读 评论(0) 收藏

    three version are provided. disjoint set, linked list version with weighted-union heuristic, rooted ...

  3. hdu 1031 (partial sort problem, nth_element, stable_partition, lambda expression) 分类: hdoj 2015-06-15 17:47 26人阅读 评论(0) 收藏

    partial sort. first use std::nth_element to find pivot, then use std::stable_partition with the pivo ...

  4. APP被苹果APPStore拒绝的各种原因 分类: ios相关 app相关 2015-06-25 17:27 200人阅读 评论(0) 收藏

    APP被苹果APPStore拒绝的各种原因 1.程序有重大bug,程序不能启动,或者中途退出. 2.绕过苹果的付费渠道,我们之前游戏里的用兑换码兑换金币. 3.游戏里有实物奖励的话,一定要说清楚,奖励 ...

  5. Hdu428 漫步校园 2017-01-18 17:43 88人阅读 评论(0) 收藏

    漫步校园 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

  6. Java中的日期操作 分类: B1_JAVA 2015-02-16 17:55 6014人阅读 评论(0) 收藏

    在日志中常用的记录当前时间及程序运行时长的方法: public void inject(Path urlDir) throws Exception { SimpleDateFormat sdf = n ...

  7. The 3n + 1 problem 分类: POJ 2015-06-12 17:50 11人阅读 评论(0) 收藏

    The 3n + 1 problem Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53927   Accepted: 17 ...

  8. POJ3258 River Hopscotch 2017-05-11 17:58 36人阅读 评论(0) 收藏

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13598   Accepted: 5791 ...

  9. Speed Limit 分类: POJ 2015-06-09 17:47 9人阅读 评论(0) 收藏

    Speed Limit Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 17967   Accepted: 12596 Des ...

随机推荐

  1. struts2的异常配置

    1:当我们出现异常我们浏览器会直接暴露我们的技术结构,会给我们的项目带来一些安全隐患.2:当这种错误出现,给用户感觉是非常不友好.3:怎么解决 1:如果处理找不到action方法的错误呢? 在Stru ...

  2. 启用SQL Server 2008的专用管理员连接(DAC)

    参考:http://technet.microsoft.com/zh-cn/library/ms178068(v=SQL.105).aspx 问题: 一个在我们公司实习的DBA向我询问如何开启SQL ...

  3. Python 实践项目 游戏

    https://www.zhihu.com/collection/92700207?page=1

  4. docker registry ui

    https://hub.docker.com/r/parabuzzle/docker-registry-ui/

  5. linux 安装SAMtools,bcftools,htslib,sratoolkit,bedtools,GATK,TrimGalore,qualimap,vcftools,bwa

    --------------------安装Samtools---------------------------------------------------------------------- ...

  6. SpringBoot配置文件YML 注意事项

    YML读取注意事项 使用YML时遇到的坑: 最近在做项目时,遇到了一些在读取YML配置时发生的问题,在这里写一并写下来,希望给自己以及大家一个提示,能尽量避免在读取配置文件时发生这些错误,给开发带来不 ...

  7. 最短路径Dijkstra算法(邻接矩阵)

    Dijkstra算法的原理: 从某个源点到其余各顶点的最短路径,即单源点最短路径(仅适合非负权值图).单源点最短路径是指:给定带权有向图G和源点v,求从v到G中其余各顶点的最短路径.迪杰斯特拉(Dij ...

  8. RMQ(或运算)

    RMQ https://ac.nowcoder.com/acm/contest/283/J 题目描述 按位或运算:处理两个长度相同的二进制数,两个相应的二进位中只要有一个为1,该位的结果值为1.例如5 ...

  9. 【校招面试 之 C/C++】第8题 C++中的静态绑定与动态绑定

    转自:https://blog.csdn.net/chgaowei/article/details/6427731   做了部分修改 为了支持c++的多态性,才用了动态绑定和静态绑定.理解他们的区别有 ...

  10. day7:vcp考试

    Q121. An ESXi 6.x host in the vCenter Server inventory has disconnected due to an All Paths Down (AP ...