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. Linux CPU 核数检查脚本

    #!/bin/bash physicalNumber=0 coreNumber=0 logicalNumber=0 HTNumber=0 logicalNumber=$(grep "proc ...

  2. 错误提示 Unsupported compiler 'com.apple.compilers.llvmgcc42' selected for architecture 'i386'

    转自http://blog.csdn.net/cyuyanenen/article/details/51444974 警告提示:Invalid C/C++ compiler in target Cor ...

  3. Windows命令查看文件MD5码

    D:\>certutil -hashfile md5test.txt MD5 MD5 哈希(文件 md5test.txt): d6 f6 bb 38 b5 6b 67 8f 34 9b e4 d ...

  4. HDU 4289 Control

    最小割 一个点拆成两个 AddEdge(i,i+N,x); 原图中的每条边这样连 AddEdge(u+N,v,INF); AddEdge(v+N,u,INF); S是源点,t+N是汇点.最大流就是答案 ...

  5. undefine refrence to "*******"

    windows mingw gcc  编译出现莫名的错误  wsaaddresstostringa, 理解起来,应该是link的时候,出现的问题 (在console的日志栏也能看出来) 然后,在ECL ...

  6. APK重新签名方法

    Android使用SHA1-RSA算法进行签名.可通过eclipse插件进行,可以通过keytool和jarsigner 用命令行执行,也可以在源码下进行签名. 第一种:通过使用eclipse进行签名 ...

  7. leetcode441(巧妙利用取整和解方程)

    You have a total of n coins that you want to form in a staircase shape, where every k-th row must ha ...

  8. Tomcat服务器顶层结构和启动过程【转】

    号外:2016 最流行的是哪一种 Java 应用服务器呢? 通过从部署的 1240 个 JVM 中得到的数据,我们能够确定出现了 862 个容器供应商,或者说是占到了运行环境的 70% 左右.这些容器 ...

  9. 个人linux简单笔记,随时更新

    vim显示行数 :set nu 查找文件 find /home -name config.txt 重命名文件或者文件夹 mv a b centos中phpize的安装 yum install php- ...

  10. mongodb replica set介绍

    近年来,随着大数据越来越火,非关系型数据库的重要性被越来越多的人所认知,越来越多的开发者逐渐加入到NoSQL的阵营中.我们知道NoSQL是Not Only SQL的意思,既然如此,很多关系型数据库所支 ...