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 the i-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 workout a[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
Input Output Hint
Iahub will choose exercises a[][] → a[][] → a[][] → a[][] → a[][]. Iahubina will choose exercises a[][] → a[][] → a[][] → a[][] → a[][].

题目链接:http://codeforces.com/problemset/problem/429/B

*********************************************************

题意: Iahub 和 Iahubina在一个矩阵健身房锻炼身体,Iahub从(1,1)出发,要到达(n,m);Iahubina从(n,1)出发,要到达(1,m);他们俩还必须要见上一面

解题思路:可以枚举每个可能相遇的点所产生的最大情况

dp1[i][j] 从(1,1)到(i,j)的最大值
dp2[i][j] 从(n,m)到(i,j)的最大值
dp3[i][j] 从(n,1)到(i,j)的最大值
dp1[i][j] 从(1,m)到(i,j)的最大值 ans为最终结果:
由于遍历的每个点是,可以分为四个部分
(1,1)->(i,j)+(i,j)->(n,m) + (n,1)->(i,j) + (1,m)->(i,j)

因此可以用ans更新四部分和的最大值 AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std; #define N 1200
#define INF 0x3f3f3f3f int maps[N][N],dp1[N][N],dp2[N][N],dp3[N][N],dp4[N][N]; int main()
{
int n,m,i,j; while(scanf("%d %d", &n,&m) != EOF)
{
memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp2));
memset(dp3,,sizeof(dp3));
memset(dp4,,sizeof(dp4));
memset(maps,,sizeof(maps)); for(i=; i<=n; i++)
for(j=; j<=m; j++)
scanf("%d", &maps[i][j]); for(i=; i<=n; i++)///(1,1)到(i,j)最大值
for(j=; j<=m; j++)
dp1[i][j]=max(dp1[i-][j],dp1[i][j-])+maps[i][j]; for(i=n; i>=; i--)///(n,m)到(i,j)最大值
for(j=m; j>=; j--)
dp2[i][j]=max(dp2[i+][j],dp2[i][j+])+maps[i][j]; for(i=n; i>=; i--)///(n,1)到(i,j)最大值
for(j=; j<=m; j++)
dp3[i][j]=max(dp3[i][j-],dp3[i+][j])+maps[i][j]; for(i=; i<=n; i++)///(1,m)到(i,j)最大值
for(j=m; j>=; j--)
dp4[i][j]=max(dp4[i][j+],dp4[i-][j])+maps[i][j]; int ans=;
for(i=; i<n; i++)
for(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+]);
}///(1,1)->(i,j)+(i,j)->(n,m) + (n,1)->(i,j) + (1,m)->(i,j) printf("%d\n", ans);
}
return ;
}

 

CodeForces 429 B B. Working out的更多相关文章

  1. Codeforces 429 B. Working out-dp( Codeforces Round #245 (Div. 1))

    B. Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  2. codeforces 429 On the Bench dp+排列组合 限制相邻元素,求合法序列数。

    限制相邻元素,求合法序列数. /** 题目:On the Bench 链接:http://codeforces.com/problemset/problem/840/C 题意:求相邻的元素相乘不为平方 ...

  3. CodeForces 429 B Working out(递推dp)

    题目连接:B. Working out 我想了很久都没有想到怎么递推,看了题解后试着自己写,结果第二组数据就 wa 了,后来才知道自己没有判选择的两条路径是否只是一个交点. 大概思路是:先预处理出每个 ...

  4. Codeforces 429 A. Xor-tree

    下来的第一次相遇是在不翻盖的同一节点,递归可以是.... A. Xor-tree time limit per test 1 second memory limit per test 256 mega ...

  5. CodeForces 840C - On the Bench | Codeforces Round #429 (Div. 1)

    思路来自FXXL中的某个链接 /* CodeForces 840C - On the Bench [ DP ] | Codeforces Round #429 (Div. 1) 题意: 给出一个数组, ...

  6. CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

    思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...

  7. CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)

    /* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...

  8. codeforces Round#429 (Div2)

    2017-08-20 10:00:37 writer:pprp 用头文件#include <bits/stdc++.h>很方便 A. Generous Kefa codeforces 84 ...

  9. 【Codeforces Round #429 (Div. 2) A】Generous Kefa

    [Link]:http://codeforces.com/contest/841/problem/A [Description] [Solution] 模拟,贪心,每个朋友尽量地多给气球. [Numb ...

随机推荐

  1. 大数据除法(Large data division)

    题目描述 Description 除法是计算中的基础运算,虽然规则简单,但是位数太多了,也难免会出错.现在的问题是:给定任意位数(足够大就可以啦O(∩_∩)O)的一个被除数,再给定一个long lon ...

  2. 网络通信框架Apache MINA

    Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络 ...

  3. drupal7 开发block

    在自己开发的模块的module文件中,实现两个钩子:hook_block_info()和hook_block_view() function journal_block_info() { $block ...

  4. Yii2.0官方高级模板的目录结构分析

    Yii 是什么 Yii 是一个高性能,基于组件的 PHP 框架,用于快速开发现代 Web 应用程序.名字 Yii (读作 易)在中文里有"极致简单与不断演变"两重含义,也可看作 Y ...

  5. 已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭

    已有打开的与此 Command 相关联的 DataReader,必须首先将它关闭 引用:   http://www.cnblogs.com/maxao/archive/2011/03/18/19881 ...

  6. strlcpy() 函数

    size_t strlcpy(char *dst, const char *src, size_t siz) {  char *d = dst; const char *s = src; size_t ...

  7. abs与fabs的区别

    Python有五个标准的数据类型: 数字 int(整数) long(长整数) float(浮点实数值) complex(复数) 字符串 列表 元组 字典 abs与fabs的区别  abs是求整数的绝对 ...

  8. postgres-xl 集体搭建(1)

    安装并编辑脚本 cd /opt/curl -O http://files.postgres-xl.org/postgres-xl95r1beta1.tar.gztar -zxvf postgres-x ...

  9. tomcat连接数设置

    如何加大tomcat连接数 在tomcat配置文件server.xml中的<Connector ... />配置中,和连接数相关的参数有:minProcessors:最小空闲连接线程数,用 ...

  10. java 单例模式及getInstance的好处

    1.什么是单例模式 简单理解为,有一个类,只能有一个实例化对象,这就是单例模式. 2.getInstance的好处 首先看一下怎样使用getInstance实现单例模式 public class Co ...