CodeForces 429B
Working out
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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 thei-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 workouta[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
3 3
100 100 100
100 1 100
100 100 100
800
Hint
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的网格,一个人从左上走到右下(只能往右或往下),另一个人从左下走到右上(只能往右或往上),每个格子都有一定数值,经过就可以获得该数值,两人在网格中只能相遇一次,相遇点数值两人都不能获得,求两人获得数值和的最大值。
解题思路:先预处理出从四个顶点出发到任一点的最大值,用dp即可。然后 枚举相遇点,相遇点不可能在边缘,因为那样交点就肯定不止一个,对于每个交点有两种方式走,求出两种方式最大值就可。
//2016.8.30
#include<iostream>
#include<cstdio> using namespace std; const int maxn = ;
int n, m, a[maxn][maxn];
int dp1[maxn][maxn],dp2[maxn][maxn],dp3[maxn][maxn],dp4[maxn][maxn];
//dp1[i][j] := 从 (1, 1) 到 (i, j) 的最大分数
//dp2[i][j] := 从 (i, j) 到 (n, m) 的最大分数
//dp3[i][j] := 从 (n, 1) 到 (i, j) 的最大分数
//dp4[i][j] := 从 (i, j) 到 (1, m) 的最大分数
int main()
{
while(cin>>n>>m)
{
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++)
scanf("%d", &a[i][j]);
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] = a[i][j]+max(dp3[i][j-], dp3[i+][j]); for(int i = ; i <= n; i++)
for(int j = m; j >= ; j--)
dp4[i][j] = a[i][j]+max(dp4[i][j+], dp4[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 ;
}
CodeForces 429B的更多相关文章
- CodeForces 429B Working out DP
E - Working out Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Su ...
- Codeforces 429B Working out
http://codeforces.com/contest/429/problem/B 题意:一个从左下到右上,一个从左上到右下,要求只相交一次,求整个路径和的最大值 思路:发现可以枚举交点,然后算到 ...
- Codeforces 429B Working out(递推DP)
题目链接:http://codeforces.com/problemset/problem/429/B 题目大意:两个人(假设为A,B),打算健身,有N行M列个房间,每个房间能消耗Map[i][j]的 ...
- CODEFORCES 429B 动态规划
http://codeforces.com/problemset/problem/429/B 可以参考这篇文章: http://blog.csdn.net/pure_lady/article/deta ...
- Codeforces 429B Working out:dp【枚举交点】
题目链接:http://codeforces.com/problemset/problem/429/B 题意: 给你一个n*m的网格,每个格子上有一个数字a[i][j]. 一个人从左上角走到右下角,一 ...
- CodeForces 429B Working out 动态规划
Description Summer is coming! It's time for Iahub and Iahubina to work out, as they both want to loo ...
- Codeforces 429B B. Working out
题目意思: 给n*m的矩阵,每个格子有个数,A从(1,1)出发只能向下或右走,终点为(n,m),B从(n,1)出发只能向上或右走,终点为(1,m).两个人的速度不一样,走到的格子可以获的该格子的数,两 ...
- CodeForces 429B【dp】
题意: 在一个n*m的矩阵中有两只虫子,一只从左上角向右下角移动,另外一只从左下角向右上角移动. 要求: 1.第一只虫子每次只能向左或者向下移动一格,另外一只只能向上或者向右移动一格. 2.两只虫子的 ...
- 【Codeforces 429B】Working out
[链接] 我是链接,点我呀:) [题意] 两个人,一个人在左上角,一个人在左下角. 左上角要到右下角去 左下角要到右上角去 只能走到相邻的格子(不能往回走,即一个往右下,一个往右上走) 要求这两个人必 ...
随机推荐
- C#设置word段落首行缩进为0
PublicVar.m_WordApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = ; PublicVar.m_WordApp.S ...
- html中的空白字符问题
1.当我们想使用百分比来进行两个盒子的并排 代码: <!DOCTYPE html> <html lang="en"> <head> <me ...
- iOS AFN向接口端传递JSON数据
NSDictionary *body = @{@"snippet": @{@"topLevelComment":@{@"snippet":@ ...
- 基于keepalived 实现VIP转移,lvs,nginx的高可用
转自:http://www.tuicool.com/articles/eu26Vz 一.Keepalived 高可用集群的解决方案 二.VRRP的有限状态机 三.利用keepalived 实现主从VI ...
- (简单) POJ 2253 Frogger,Dijkstra。
Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Fro ...
- Struts2实现异步调用机制详细剖析(XML和JSON)
一.使用XML传递 1.页面展示getXML.jsp <%@ page language="java" import="java.util.*" page ...
- label中添加图片
创建NSTextAttachment的对象,用来装在图片 将NSTextAttachment对象的image属性设置为想要使用的图片 设置NSTextAttachment对象bounds大小,也就是要 ...
- Extjs的架构设计思考,单页面应用 or 多页面?
写在前面:不要认为 EXTJS 高版本就是一个界面改良,在项目中,仍然用 N 张页面,在 N 张页面部署 EXTJS .这种方式不用多讲,效率问题大家都看得出来, EXTJS 是一个集成开发工具,注定 ...
- php中DateTime的format格式以及 TtoDatetime函数
Definition and Usage The date() function formats a local time/date. Syntaxdate(format,timestamp)Para ...
- Spring MVC 关于分页的简单实现
据本人了解,目前较常用的分页实现办法有两种: 1.每次翻页都修改SQL,向SQL传入相关参数去数据库实时查出该页的数据并显示. 2.查出数据库某张表的全部数据,再通过在业务逻辑里面进行处理去取得某些数 ...