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.

  1. Sample Input
  2. Input
  3.  
  4. Output
  5.  
  6. Hint
  7. 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

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

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

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

    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)的最大值
  2.  
  3. ans为最终结果:
    由于遍历的每个点是,可以分为四个部分
    1,1)->(i,j)+(i,j)->(n,m) + (n,1)->(i,j) + 1,m)->(i,j)

    因此可以用ans更新四部分和的最大值
  4. AC代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <stack>
  7. #include <map>
  8. #include <vector>
  9. using namespace std;
  10.  
  11. #define N 1200
  12. #define INF 0x3f3f3f3f
  13.  
  14. int maps[N][N],dp1[N][N],dp2[N][N],dp3[N][N],dp4[N][N];
  15.  
  16. int main()
  17. {
  18. int n,m,i,j;
  19.  
  20. while(scanf("%d %d", &n,&m) != EOF)
  21. {
  22. memset(dp1,,sizeof(dp1));
  23. memset(dp2,,sizeof(dp2));
  24. memset(dp3,,sizeof(dp3));
  25. memset(dp4,,sizeof(dp4));
  26. memset(maps,,sizeof(maps));
  27.  
  28. for(i=; i<=n; i++)
  29. for(j=; j<=m; j++)
  30. scanf("%d", &maps[i][j]);
  31.  
  32. for(i=; i<=n; i++)///(1,1)到(i,j)最大值
  33. for(j=; j<=m; j++)
  34. dp1[i][j]=max(dp1[i-][j],dp1[i][j-])+maps[i][j];
  35.  
  36. for(i=n; i>=; i--)///(n,m)到(i,j)最大值
  37. for(j=m; j>=; j--)
  38. dp2[i][j]=max(dp2[i+][j],dp2[i][j+])+maps[i][j];
  39.  
  40. for(i=n; i>=; i--)///(n,1)到(i,j)最大值
  41. for(j=; j<=m; j++)
  42. dp3[i][j]=max(dp3[i][j-],dp3[i+][j])+maps[i][j];
  43.  
  44. for(i=; i<=n; i++)///(1,m)到(i,j)最大值
  45. for(j=m; j>=; j--)
  46. dp4[i][j]=max(dp4[i][j+],dp4[i-][j])+maps[i][j];
  47.  
  48. int ans=;
  49. for(i=; i<n; i++)
  50. for(j=; j<m; j++)
  51. {
  52. ans=max(ans,dp1[i][j-]+dp2[i][j+]+dp3[i+][j]+dp4[i-][j]);
  53. ans=max(ans,dp1[i-][j]+dp2[i+][j]+dp3[i][j-]+dp4[i][j+]);
  54. }///(1,1)->(i,j)+(i,j)->(n,m) + (n,1)->(i,j) + (1,m)->(i,j)
  55.  
  56. printf("%d\n", ans);
  57. }
  58. return ;
  59. }
  1.  
  1.  

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. windows 环境下搭建django 错误分析总结

    最近对于python核心编程学习完后,想进一步学习django的web开发,考虑再三还是决定在本机(win7)上搭建环境. 刚接触难免会出现问题,最大的一个问题是安装完django的包后,在cmd命令 ...

  2. 【Machine Learning in Action --4】朴素贝叶斯从个人广告中获取区域倾向

    背景:广告商往往想知道关于一个人的一些特定人口统计信息,以便能更好地定向推销广告. 我们将分别从美国的两个城市中选取一些人,通过分析这些人发布的信息,来比较这两个城市的人们在广告用词上是否不同.如果结 ...

  3. ADO.NET基础、数据增删改查

    ADO.NET:数据访问技术,就是将C#和MSSQL连接起来的一个纽带.我们可以通过ADO.NET将内存中的临时数据写入到数据库中,也可以将数据库中的数据提取到内存中供程序调用. 数据库数据的增.删. ...

  4. Python 学习笔记3

    人如果从来没有怀疑过自己,那他永远都不会进步. 今天学习Python解释器及其环境. http://www.pythondoc.com/pythontutorial3/interpreter.html

  5. Python 修炼1

    2016年11月21日 Python基础修炼第一篇 1.Python是什么?有什么优缺点呢? python是一个高级编程语言. 优点:开发效率比较高,不但有php写网页的功能,还有写后台的功能 缺点: ...

  6. mongoDB4--mongoDB的增删改查

    MongoDb基本操作之增删改查我们知道传统关系型数据库的最常用操作就是"增加/删除/修改/查询",也就是传说中的CRUD(create/remove/updte/delete). ...

  7. Linux相关

    可以用secureCRT远程连接Linux系统,进入tomcat,并且启动tomcat:bin/startup.sh 查看实时日志状态:tail -f logs/catalina.out

  8. hadoop pig入门总结

    在这里贴一个pig源码的分析,做pig很长时间没做笔记,不包含任何细节,以后有机会再说吧 http://blackproof.iteye.com/blog/1769219 hadoop pig入门总结 ...

  9. [笔记]Practical Lessons from Predicting Clicks on Ads at Facebook

    ABSTRACT 这篇paper中作者结合GBDT和LR,取得了很好的效果,比单个模型的效果高出3%.随后作者研究了对整体预测系统产生影响的几个因素,发现Feature+Model的贡献程度最大,而其 ...

  10. django搭建Bootstrap常用问题解决方法

    1.进入页面,提示Creating a ModelForm without either the 'fields' attribute or the 'exclude'时 解决方法:打开forms.p ...