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
[链接] 我是链接,点我呀:) [题意] 两个人,一个人在左上角,一个人在左下角. 左上角要到右下角去 左下角要到右上角去 只能走到相邻的格子(不能往回走,即一个往右下,一个往右上走) 要求这两个人必 ...
随机推荐
- strace 分析 跟踪 进程错误
strace是什么? 按照strace官网的描述, strace是一个可用于诊断.调试和教学的Linux用户空间跟踪器.我们用它来监控用户空间进程和内核的交互,比如系统调用.信号传递.进程状态变更等. ...
- java 读excel xlsx
http://bbs.csdn.net/topics/380257685 import java.io.File; import java.io.IOException; import java.io ...
- 用Quick Cocos2dx做一个连连看(三)
做个日记吧. 最近比较忙,斗志也不高,昨天有点时间了,开始做了一下连接方法,一开始用的搜索算法,但是bug比较多,究其原因是对语法和算法都不是很熟悉. 然后昨天下午利用点时间用稍微通俗一点的连接算法, ...
- pymongo一次更新多条数据
db.collection.update(query, update, upsert, multi) pymongo使用示例 db.collection.update({}, {'$set' : {' ...
- php学习记录
放了寒假.期末考试折腾了一个月都不会写代码了. 一放寒假就找了套PHP培训的课程 在这做些笔记,系统的学习一下php 2017.1.14 介绍集成环境---wamp Apache服务器根目录 / -- ...
- NSString的几个方法(rangeOfString,hasPrefix,hasSuffix,改变大小写...)
- (NSRange)rangeOfString:(NSString *)searchString;//查找字符串中是包涵在某个字符串,并返回其开始位置和长度 例: NSRange range = [ ...
- java类固定值代替基表写法
package cn.com.mcd.enumeration; public enum AuditStatusEnum { NOTAUDIT("0", "未审核" ...
- 查看错误日志发现有两个警告(ignored in --skip-name-resolve mode)
2016-08-02 17:30:26 17374 [Warning] 'user' entry '@losnau-223.com' ignored in --skip-name-resolve mo ...
- Tsinsen-1486:树【Trie树 + 点分治】
暴力部分: 这个题一开始的想法是 n^2 枚举两个点,然后logn维护LCA,在倍增的同时维护异或值和 k 的个数. s_z_l老爷指导了新的思路,既然这个树只有n^2个LCA,那么枚举LCA,同时向 ...
- onethink微博插件雏形记
2014年7月30日 17:08:44 后台微博插件: 一.功能: 1.绑定微博 2.发布的文章自动发布到新浪微博 3.插件独立性强,修改地方少 二.效果: 插件目录 工程地址:http://down ...