cf429B dp递推
Description
Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the beach. The gym where they go is a matrix a with n lines and m columns. Let number a[i][j] represents the calories burned by performing workout at the cell of gym in thei-th line and the j-th column.
Iahub starts with workout located at line 1 and column 1. He needs to finish with workout a[n][m]. After finishing workout a[i][j], he can go to workout a[i + 1][j] or a[i][j + 1]. Similarly, Iahubina starts with workout a[n][1] and she needs to finish with workouta[1][m]. After finishing workout from cell a[i][j], she goes to either a[i][j + 1] or a[i - 1][j].
There is one additional condition for their training. They have to meet in exactly one cell of gym. At that cell, none of them will work out. They will talk about fast exponentiation (pretty odd small talk) and then both of them will move to the next workout.
If a workout was done by either Iahub or Iahubina, it counts as total gain. Please plan a workout for Iahub and Iahubina such as total gain to be as big as possible. Note, that Iahub and Iahubina can perform workouts with different speed, so the number of cells that they use to reach meet cell may differs.
Input
The first line of the input contains two integers n and m (3 ≤ n, m ≤ 1000). Each of the next n lines contains m integers: j-th number from i-th line denotes element a[i][j] (0 ≤ a[i][j] ≤ 105).
Output
The output contains a single number — the maximum total gain possible.
Sample Input
3 3 100 100 100 100 1 100 100 100 100
800
代码:注意一点,交点不能在边界,如果在边界不满足两人只相遇一次的条件。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std; int mp[][];
int dp1[][],dp2[][],dp3[][],dp4[][]; int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==)
{
for(int i=; i<=n; i++)
for(int j=; j<=m; j++)
scanf("%d",&mp[i][j]);
memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp2));
memset(dp3,,sizeof(dp3));
memset(dp4,,sizeof(dp4));
for(int i=; i<=n; i++)//右下
for(int j=; j<=m; j++)
dp1[i][j]=max(dp1[i-][j],dp1[i][j-])+mp[i][j];
for(int i=n; i>=; i--)//右上
for(int j=; j<=m; j++)
dp2[i][j]=max(dp2[i+][j],dp2[i][j-])+mp[i][j];
for(int i=; i<=n; i++)//左下
for(int j=m; j>=; j--)
dp3[i][j]=max(dp3[i-][j],dp3[i][j+])+mp[i][j];
for(int i=n; i>=; i--)//左上
for(int j=m; j>=; j--)
dp4[i][j]=max(dp4[i+][j],dp4[i][j+])+mp[i][j];
int ans=;
for(int i=; i<n; i++)
for(int j=; j<m; j++)
{
ans=max(ans,dp1[i-][j]+dp2[i][j-]+dp3[i][j+]+dp4[i+][j]);
ans=max(ans,dp1[i][j-]+dp2[i+][j]+dp3[i-][j]+dp4[i][j+]);
}
printf("%d\n",ans);
}
return ;
}
cf429B dp递推的更多相关文章
- hdu2089(数位DP 递推形式)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu 2604 Queuing(dp递推)
昨晚搞的第二道矩阵快速幂,一开始我还想直接套个矩阵上去(原谅哥模板题做多了),后来看清楚题意后觉得有点像之前做的数位dp的水题,于是就用数位dp的方法去分析,推了好一会总算推出它的递推关系式了(还是菜 ...
- Power oj2498/DP/递推
power oj 2498 /递推 2498: 新年礼物 Time Limit: 1000 MS Memory Limit: 65536 KBTotal Submit: 12 Accepted: 3 ...
- BZOJ4321queue2——DP/递推
题目描述 n 个沙茶,被编号 1~n.排完队之后,每个沙茶希望,自己的相邻的两 人只要无一个人的编号和自己的编号相差为 1(+1 或-1)就行: 现在想知道,存在多少方案满足沙茶们如此不苛刻的条件. ...
- Shell Necklace (dp递推改cdq分治 + fft)
首先读出题意,然后发现这是一道DP,我们可以获得递推式为 然后就知道,不行啊,时间复杂度为O(n2),然后又可以根据递推式看出这里面可以拆解成多项式乘法,但是即使用了fft,我们还需要做n次多项式乘法 ...
- hdu 1723 DP/递推
题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacc ...
- UVA 10559 Blocks(区间DP&&递推)
题目大意:给你玩一个一维版的消灭星星,得分是当前消去的区间的长度的平方,求最大得分. 现在分析一下题目 因为得分是长度的平方,不能直接累加,所以在计算得分时需要考虑前一个状态所消去的长度,仅用dp[l ...
- [NOI2009]管道取珠 DP + 递推
---题面--- 思路: 主要难点在思路的转化, 不能看见要求$\sum{a[i]^2}$就想着求a[i], 我们可以对其进行某种意义上的拆分,即a[i]实际上可以代表什么? 假设我们现在有两种取出某 ...
- HDU 2154 跳舞毯 | DP | 递推 | 规律
Description 由于长期缺乏运动,小黑发现自己的身材臃肿了许多,于是他想健身,更准确地说是减肥. 小黑买来一块圆形的毯子,把它们分成三等分,分别标上A,B,C,称之为“跳舞毯”,他的运动方式是 ...
随机推荐
- Solr安装过程
Solr安装过程 下载相关资料 solr 4.2.0 http://lucene.apache.org/solr/ 期间安装过 solr 4.3.0 很可惜没有配置成功 apache-tomcat-7 ...
- 解决 spring mvc 3.0 结合 hibernate3.2 使用<tx:annotation-driven>声明式事务无法提交的问题(转载)
1.问题复现 spring 3.0 + hibernate 3.2 spring mvc使用注解方式:service使用@service注解 事务使用@Transactional 事务配置使用 < ...
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- XLL 框架库中的函数
这个框架库,可以让编写 XLL 更加容易.包含了管理 XLOPER/XLOPER12 内存的简单函数,创建临时 XLOPER/XLOEPR12 ,强制调用回调函数 (Excel4,Excel4v,Ex ...
- [Android Pro] Android studio jni中调用Log输出调试信息
reference to : http://www.linuxidc.com/Linux/2014-02/96341.htm Android 开发中,java 可以方便的使用调试信息Log.i, Lo ...
- [Android Pro] Android保存图片到系统图库
http://stormzhang.github.io/android/2014/07/24/android-save-image-to-gallery/ http://blog.csdn.net/x ...
- August 20th 2016 Week 34th Saturday
Everything you see exists together in a delicate balance. 你所看到的一切都处于微妙的平衡中. Seeking for balance in l ...
- CLR via C#(11)-无参属性、有参数属性(索引器)
一. 无参属性 1. 定义属性 无参属性就是我们最常见的属性方式,在赋值时可以加入一定的逻辑判断.属性的定义其实不复杂,先看个直观的例子: 说明: 属性要定义名称和类型,且类型不能是void. 属性是 ...
- CSS3–2.css3 响应式布局
1.响应式布局 响应式布局是现在很流行的一个设计理念,随着移动互联网的盛行,为解决如今各式各样的浏览器分辨率以及不同移动设备的显示效果,设计师提出了响应式布局的设计方案.所谓的响应式布局,就是一个网站 ...
- volatile关键字与线程间通信
>>Java内存模型 现在计算机普遍使用多处理器进行运算,并且为了解决计算机存储设备和处理器的运算速度之间巨大的差距,引入了高速缓存作为缓冲,缓存虽然能极大的提高性能,但是随之带来的缓存一 ...