CF 429B B.Working out 四个角递推
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 四个角递推的更多相关文章
- 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 ...
- Android 二维码扫描框 加四个角及中间横线自动下滑
红色为加四个角 黄色为扫描线自动下滑 /* * Copyright (C) 2008 ZXing authors * * Licensed under the Apache License, Ver ...
- unity3d 获取相机视口四个角的坐标
功能:如标题所示,主要考虑用来做3d Plane的自适应屏幕 /// <summary> /// 获取指定距离下相机视口四个角的坐标 /// </summary> /// &l ...
- [转]JAVA 根据经纬度算出附近的正方形的四个角的经纬度
csv文件转化为geojson文件中,涉及到路测图的打点生成,打点是由一个个正方形组成,而正方形是由四个点组成的,这四个点根据经纬度和范围生成,具体的实现代码是从网上找来的: /** * * @par ...
- Bootstrap3基础 img-rounded 图片的四个角改成圆角
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- HTML和JS完成页面点击四个角弹出管理页面
实现方法1: HTML代码: <div class="top-left-corner"></div> <div class="top-rig ...
- 四角递推(CF Working out,动态规划递推)
题目:假如有A,B两个人,在一个m*n的矩阵,然后A在(1,1),B在(m,1),A要走到(m,n),B要走到(1,n),两人走的过程中可以捡起格子上的数字,而且两人速度不一样,可以同时到一个点(哪怕 ...
- cf 429B Working out(简单dp)
B. Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- GMap获取可视范围内四个角的坐标
原理: 先获取控件的四个顶点,逐一将其转换成经纬度坐标. private void GetBonds() { //左上↖ PointLatLng pLeftTop = map1.FromLocalTo ...
随机推荐
- 阿里云nginx+thinkphp环境运行会直接下载php文件的问题。
测试了好久,在网上查了各种nginx.conf的虚拟机配置都没用 其实最简单的方法就是在php.ini中找到 #cgi.fix_pathinfo=1; 去掉前面的#,如果值不是1则改为1
- ORACLE 36进制和10进制,互相转换函数
第一部分 --36转10进制 create or replace function f_36to10 (str varchar) return int is returnValue int; s ...
- WPF popup控件的使用
<Window x:Class="WPFPopup.RuntimePopup" xmlns="http://schemas.microsoft.com/wi ...
- Unable to run Kiwi tests on iOS8 device
本文转载至 http://stackoverflow.com/questions/25871601/unable-to-run-kiwi-tests-on-ios8-device 5down vote ...
- P1355 神秘大三角
题目描述 判断一个点与已知三角形的位置关系. 输入输出格式 输入格式: 前三行:每行一个坐标,表示该三角形的三个顶点 第四行:一个点的坐标,试判断该点与前三个点围成三角形的位置关系 (详见样例) 所有 ...
- [Phoenix] 八、动态列
摘要: 传统关系型数据库的动态列实现只能依赖逻辑层的设计实现,而Phoenix是HBase上的SQL层,借助HBase特性实现的动态列功能,具有高度的灵活性,告别业务逻辑层的复杂设计. 一.概要 动态 ...
- Javascript高级程序设计笔记(很重要尤其是对象的设计模式与继承)
var obj = {'a':'a'}; var fun = function (){} console.log(typeof obj);//object console.log(typeof fun ...
- ubuntu动态加载模块简单模板
1:简单代码 #include<linux/init.h> #include<linux/module.h> MODULE_LICENSE("GPL"); ...
- undefined reference to '__android_log_print'解决方案
1:在源程序中添加头文件 #include <cutils/log.h> 2:在Android.mk中添加 LOCAL_SHARED_LIBRARIES := \ libutils \ l ...
- android——array中设置选项
Android中,R.array是提取XML资源文件中String数组的方法.具体定义和提取的方法如下: 1)在R.array中定义字符数组 <?xml version="1.0&qu ...