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
[链接] 我是链接,点我呀:) [题意] 两个人,一个人在左上角,一个人在左下角. 左上角要到右下角去 左下角要到右上角去 只能走到相邻的格子(不能往回走,即一个往右下,一个往右上走) 要求这两个人必 ...
随机推荐
- JQuery的隐式迭代和each函数和map函数
1.JQuery选择器选择出来的是一个数组对象,可是给这些每一个元素都要设置内容时,就会隐式迭代,JQuery自己实现内部循环给每个元素绑定上设置. 2.如果是获取的话,那就是默认获取第一个元素的值. ...
- pthread_join
摘要:pthread_join使一个线程等待另一个线程束. 代码中如果没有pthread_join主线程会很快结束从而使整个进程结束,从而使创建的线程没有机会开始执行就结束了.加入pthread_jo ...
- 公司搬家,拿了个费机器,没root密码,又忘了怎么搞了,
grub中找到ro->rw single init=/bin/bash passwd root
- 用saxon框架对xml数据进行过滤 - 程序员的天堂 - ITeye技术网站
用saxon框架对xml数据进行过滤 博客分类: Java Saxon 是一个 XSLT 和XQuery处理器.它是使用 XML 文档和样式表作为输入,然后生成结果文档作为输出的程序,它还包括了一 ...
- window.location.href 和 document.location.href
document表示的是一个文档对象,window表示的是一个窗口对象,一个窗口下可以有多个文档对象. 所以一个窗口下只有一个window.location.href,但是可能有多个document. ...
- Ubuntu下搭建C++开发环境
Ubuntu使用eclipse搭建c/c++编译环境----CDT插件 Ubuntu(Linux)使用Eclipse搭建C/C++编译环境 这两天,给自己电脑弄了双系统,除了原来的W ...
- MonkeyRecorder
http://www.cnblogs.com/lynn-li/p/5894953.html
- sql server 2008 学习笔记
sql server 2008 删除已有的实例 想从setup.exe中区卸载,没找到. 原来还是要从控制面板中卸载,卸载Microsoft SQL Server 2008 卸载界面会提示让你选择要删 ...
- docker网络访问(三)
docker网络访问 ifconfig查看网卡,启动docker的时候,docker会帮我们创建一个docker0的网桥. 1.随机映射 docker run -P 2.指定映射 -p hostPor ...
- Jquery Validate 正则表达式实用验证代码常用的
jQuery.validate 的正则验证功能,包括手机号码.电话号码.邮政编码.QQ号码.IP地址.字母和数字.中文的验证等 手机号码验证 以下为引用内容: 代码如下: jQuery.validat ...