B. Working out
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.

Examples
input
Copy
3 3
100 100 100
100 1 100
100 100 100
output
800
Note
Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].

题意:给出n*m的方格,a从左上到右下,b从左下到右下,a只能往右往下走,b只能往右往上走,减去重合的一个交叉点值,求a和b走过的路径权值最大值

题解:分析交叉点的位置 肯定不在四个边上,否则会存在多个交叉点,当a b位于交叉点,只有俩种情况:

1) A向右走,相遇后继续向右走,而B向上走,相遇后继续向上走 
2) A向下走,相遇后继续向下走,而B向右走,相遇后继续向右走

然后枚举四个角的dp值

最后max求最大值

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=;
int a[maxn][maxn];
int dp1[maxn][maxn];
int dp2[maxn][maxn];
int dp3[maxn][maxn];
int dp4[maxn][maxn];
int main()
{
int n,m;
while(cin>>n>>m){
if(n==||m==)
{
break;
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
cin>>a[i][j];
}
}
memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp3));
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-])+a[i][j];//左上方
}
}
for(int i=n;i>=;i--){
for(int j=m;j>=;j--){
dp2[i][j] = max(dp2[i+][j],dp2[i][j+])+a[i][j];//右下方
}
}
for(int i=n;i>=;i--){
for(int j=;j<=m;j++){
dp3[i][j] = max(dp3[i+][j],dp3[i][j-])+a[i][j];//左下方
}
}
for(int i=;i<=n;i++){
for(int j=m;j>=;j--){
dp4[i][j] = max(dp4[i-][j],dp4[i][j+])+a[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]);//左 右 上 下
}
}
cout<<ans<<endl;
}
return ;
}

CF 429B B.Working out 四个角递推的更多相关文章

  1. Code Force 429B Working out【递推dp】

    Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to look hot at the ...

  2. Android 二维码扫描框 加四个角及中间横线自动下滑

    红色为加四个角  黄色为扫描线自动下滑 /* * Copyright (C) 2008 ZXing authors * * Licensed under the Apache License, Ver ...

  3. unity3d 获取相机视口四个角的坐标

    功能:如标题所示,主要考虑用来做3d Plane的自适应屏幕 /// <summary> /// 获取指定距离下相机视口四个角的坐标 /// </summary> /// &l ...

  4. [转]JAVA 根据经纬度算出附近的正方形的四个角的经纬度

    csv文件转化为geojson文件中,涉及到路测图的打点生成,打点是由一个个正方形组成,而正方形是由四个点组成的,这四个点根据经纬度和范围生成,具体的实现代码是从网上找来的: /** * * @par ...

  5. Bootstrap3基础 img-rounded 图片的四个角改成圆角

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  6. HTML和JS完成页面点击四个角弹出管理页面

    实现方法1: HTML代码: <div class="top-left-corner"></div> <div class="top-rig ...

  7. 四角递推(CF Working out,动态规划递推)

    题目:假如有A,B两个人,在一个m*n的矩阵,然后A在(1,1),B在(m,1),A要走到(m,n),B要走到(1,n),两人走的过程中可以捡起格子上的数字,而且两人速度不一样,可以同时到一个点(哪怕 ...

  8. cf 429B Working out(简单dp)

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

  9. GMap获取可视范围内四个角的坐标

    原理: 先获取控件的四个顶点,逐一将其转换成经纬度坐标. private void GetBonds() { //左上↖ PointLatLng pLeftTop = map1.FromLocalTo ...

随机推荐

  1. NTAG 标签

    NTAG 标签 这里描述针对 NTAG213.而 NTAG215/216只是容量不同,其它功能都一样.  UID UID 有 7 bytes.上图中有 9 bytes 的 serial number ...

  2. Html调用 QQ接口

    <A href="tencent://message/?uin=1805843351&Site=有事Q我&Menu=yes"> <img styl ...

  3. Qt 5.5.0 Windows环境搭建

    1)訪问官方站点:http://www.qt.io/download-open-source/ 2)选择离线安装包 3)选择 Windows 离线安装包(32 位或 64 位都可用,Windows 6 ...

  4. 1185: [HNOI2007]最小矩形覆盖

    1185: [HNOI2007]最小矩形覆盖 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 1426  Solve ...

  5. EasyDarwin开源平台直播架构

    Created with Raphaël 2.1.0ClientClientEasyCMSEasyCMSEasyCameraEasyCameraEasyDarwinEasyDarwin请求设备列表设备 ...

  6. Theseven relationsarein threecategories:equivalent, congruent, andsimilar.

    http://www.math.pitt.edu/~xfc/math2370/chap5.pdf

  7. mongodb学习之:数据库命令以及固定集合

    如何我们要删除一个集合,可以采用db.test.drop()的方式,其实在这背后,这个函数运行的是drop命令.可以用runCommand达到同样的效果. 我们首先新建一个集合作为测试使用: > ...

  8. 利用ES6中的Proxy和Reflect 实现简单的双向数据绑定

    利用ES6中的Proxy (代理) 和 Reflect 实现一个简单的双向数据绑定demo. 好像vue3也把 obj.defineProperty()  换成了Proxy+Reflect. 话不多说 ...

  9. 配置composer代理

    composer config -g repo.packagist composer https://packagist.phpcomposer.com

  10. underscore.js中模板函数应用

    一.使用技术要点 (1)使用zepto.js的ajax请求; (2)使用underscore.js的_.template设定模板,模板一般以<script type="text/tem ...