Beans

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

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;

java超时了。。。c就不会

代码:

import java.util.Scanner;

public class Beans {
static int a[] = new int[], dp[] = new int[], DP[] = new int[], b[] = new int[];
public static void main(String[] argvs){
int M, N;
Scanner cin = new Scanner(System.in);
while(cin.hasNext()){
M = cin.nextInt();
N = cin.nextInt();
for(int i = ; i < M; i++){
DP[i] = ;
for(int j = ; j < N; j++){
dp[j] = ;
a[j] = cin.nextInt();
if(j < )
dp[j] = a[j];
else
dp[j] = Math.max(dp[j - ] + a[j], dp[j - ]);
}
DP[i] = dp[N - ];
if(i < )
b[i] = DP[i];
else
b[i] = Math.max(b[i - ] + DP[i], b[i - ]);
}
System.out.println(b[M - ]);
} }
}

Beans(dp,两次dp)的更多相关文章

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

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

  2. hdu5406 CRB and Apple dp+两个LIS

    题意转换为:给定n个数,求两个最长的不相交的LIS. 先说经典题一个LIS的nlogn做法.枚举当前数,若比末尾数大,插入末尾,否则二分查找,插入合适位置. 通过此题,我们有了一个用树状数组或线段树+ ...

  3. hdu 5282 Senior&#39;s String 两次dp

    题链:http://acm.hdu.edu.cn/showproblem.php?pid=5282 Senior's String Time Limit: 2000/1000 MS (Java/Oth ...

  4. UVA 10163 Storage Keepers(两次DP)

    UVA 10163 Storage Keepers(两次DP) http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Ite ...

  5. 两道dp

    链接:https://ac.nowcoder.com/acm/contest/186/C?&headNav=www 来源:牛客网终于Alice走出了大魔王的陷阱,可是现在傻傻的她忘了带武器了, ...

  6. UVA 580 Critical Mass (两次dp)

    题意:一个字符串有n个位置每个位置只可能是L或者U,问你在所有可能出现的字符串中最少出现一次三个U连在一起的字符串的个数 题解:首先从左向右枚举每个位置i,保证i,i+1,i+2是U,并且i+2(不包 ...

  7. DP(两次) UVA 10163 Storage Keepers

    题目传送门 /* 题意:(我懒得写,照搬网上的)有n个仓库,m个人看管.一个仓库只能由一个人来看管,一个人可以看管多个仓库. 每个人有一个能力值pi,如果他看管k个仓库,那么所看管的每个仓库的安全值为 ...

  8. 两道DP,四年修一次路

    第十一届:山区修路 题目描述 SNJ位于HB省西部一片群峰耸立的高大山地,横亘于A江.B水之间,方圆数千平方公里,相传上古的神医在此搭架上山采药而得名.景区山峰均在海拔3000米以上,堪称" ...

  9. Java实现 LeetCode 787 K 站中转内最便宜的航班(两种DP)

    787. K 站中转内最便宜的航班 有 n 个城市通过 m 个航班连接.每个航班都从城市 u 开始,以价格 w 抵达 v. 现在给定所有的城市和航班,以及出发城市 src 和目的地 dst,你的任务是 ...

随机推荐

  1. Qt Mac 下软件Release 公布dmg

    1.首先当然是用Qt Creator.编译一个Release版本号的软件 注意到编译出来的大小非常小,才420KB,由于一些类库还没包括进去的原因.如今还仅仅能在你本地执行,复制到其它Mac电脑就不能 ...

  2. poj 2041 Unreliable Message 字符串处理

    水的问题.直接附着到代码. //poj 2041 //sep9 #include <iostream> using namespace std; char mode[128]; char ...

  3. JavaScript 函数方法 - toString()

    Function.prototype.toString() 返回函数代码的字符串形式. 描述 Function 对象覆盖了从 Object 继承来的 Object.prototype.toString ...

  4. FineUI模拟树下拉列表

    模拟树的下拉列表 很多时候,我们希望在下拉列表中显示简单树状的层次结构,在菜单设置.机构设置等场景下这个需求尤为突出.也是基于项目需求的考虑,FineUI增加了模拟树的下拉列表的功能,显示效果如下所示 ...

  5. VPN错误789:L2TP连接尝试失败

    VPN 错误789:l2tp 连接尝试失败,因为安全层在初始化与远程计算机的协商时遇到一个处理错误 Windows配置VPN,选择“使用IPsec的第2层隧道协议(L2TP/IPSec)”时, XP系 ...

  6. C#不用COM组件导出数据到Excel中

    <?xml version='1.0'?><?mso-application progid='Excel.Sheet'?><Workbook xmlns='urn:sch ...

  7. hdu 油菜花王国

    Problem Description 程序设计竞赛即将到来,作为学校ACM集训队主力,小明训练一直很努力.今天天气不错,教练也心情大好,破例给各位队员放假一天,小明就骑着自己的小电驴到郊外踏青去了. ...

  8. Keil C减小代码编译量大小的方法(gai)

    keil-C减小代码编译大小的方法整理 方法一:(通过优化代码减小) 1.1少做乘除运算,使用左/右移位来实现乘除 Eg ,普通:a = 0x80*4: 优化:a = 0x80<<2: 1 ...

  9. (原)ubuntu16重装显卡驱动后,torch中的问题

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6030232.html 参考网址: https://github.com/torch/cutorch/i ...

  10. 一句SQL实现MYSQL的递归查询

    众所周知,目前的mysql版本中并不支持直接的递归查询,但是通过递归到迭代转化的思路,还是可以在一句SQL内实现树的递归查询的.这个得益于Mysql允许在SQL语句内使用@变量.以下是示例代码. 创建 ...