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 beach. The gym where they go is a matrix a with n lines and mcolumns. 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.
Example
3 3
100 100 100
100 1 100
100 100 100
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].
题意:一个人从左上角走到右下角,另一个人从左下角走到右上角,格子上有数,求两个人得到的最大格子数之和为多少。两人只能在一个格子相遇,这个格子的值不计入最后结果。(当然,他们一个只能向下和向右走,另一个只能向上和向右走。)
我觉得这道题好棒!虽然不会。实在不知道这怎么dp。
我没有发现一个重要的性质:当两人相遇后,他们只能沿各自原来的方向走。
画个示意图:


按照规则俩人相遇后只能按照原来的方向走才不会有多个交点,这就推出他们在边界不可能相遇。所以每次O((n-1)^2)的复杂度枚举交点,按四个方向递推dp求解。至于为什么要四个方向dp而不是两个,可以对照上图或者手动画一画想想。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN = ;
int dp1[MAXN][MAXN], dp2[MAXN][MAXN], dp3[MAXN][MAXN], dp4[MAXN][MAXN];
int a[MAXN][MAXN];
int ans; int max(int a, int b)
{
if (a < b) return b;
return a;
} int main()
{
int n, m;
cin >> n >> m;
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
cin >> a[i][j];
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++)
dp1[i][j] = a[i][j] + max(dp1[i - ][j], dp1[i][j - ]);
for (int i = n; i >= ; i--)
for (int j = m; j >= ; j--)
dp2[i][j] = a[i][j] + max(dp2[i + ][j], dp2[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 + ]);
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 ;
}
参考博客(感谢~):
【1】:http://blog.csdn.net/qq_34374664/article/details/54577940
Code Force 429B Working out【递推dp】的更多相关文章
- 递推DP URAL 1167 Bicolored Horses
题目传送门 题意:k个马棚,n条马,黑马1, 白马0,每个马棚unhappy指数:黑马数*白马数,问最小的unhappy值是多少分析:dp[i][j] 表示第i个马棚放j只马的最小unhappy值,状 ...
- 递推DP URAL 1017 Staircases
题目传送门 /* 题意:给n块砖头,问能组成多少个楼梯,楼梯至少两层,且每层至少一块砖头,层与层之间数目不能相等! 递推DP:dp[i][j] 表示总共i块砖头,最后一列的砖头数是j块的方案数 状态转 ...
- 递推DP URAL 1260 Nudnik Photographer
题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...
- 递推DP URAL 1353 Milliard Vasya's Function
题目传送门 /* 题意:1~1e9的数字里,各个位数数字相加和为s的个数 递推DP:dp[i][j] 表示i位数字,当前数字和为j的个数 状态转移方程:dp[i][j] += dp[i-1][j-k] ...
- 递推DP URAL 1119 Metro
题目传送门 /* 题意:已知起点(1,1),终点(n,m):从一个点水平或垂直走到相邻的点距离+1,还有k个抄近道的对角线+sqrt (2.0): 递推DP:仿照JayYe,处理的很巧妙,学习:) 好 ...
- 递推DP 赛码 1005 Game
题目传送门 /* 递推DP:官方题解 令Fi,j代表剩下i个人时,若BrotherK的位置是1,那么位置为j的人是否可能获胜 转移的时候可以枚举当前轮指定的数是什么,那么就可以计算出当前位置j的人在剩 ...
- 递推DP HDOJ 5328 Problem Killer
题目传送门 /* 递推DP: 如果a, b, c是等差数列,且b, c, d是等差数列,那么a, b, c, d是等差数列,等比数列同理 判断ai-2, ai-1, ai是否是等差(比)数列,能在O( ...
- hdu1978(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1978 分析: 递推DP. dp[][]表示可以到达改点的方法数. 刚开始:外循环扫描所有点dp[x][ ...
- 递推DP URAL 1031 Railway Tickets
题目传送门 /* 简单递推DP:读题烦!在区间内的都更新一遍,dp[]初始化INF 注意:s1与s2大小不一定,坑! 详细解释:http://blog.csdn.net/kk303/article/d ...
随机推荐
- NDK(23) 使用CMake 构建 c/c++代码库
1.官网 https://developer.android.com/studio/projects/add-native-code.html 2.android studio 安装相关工具 在打开的 ...
- Win7+AMD+VS2013+opencl1.x安装与测试
参考资料:http://www.cnblogs.com/lihao602/archive/2013/05/08/3067239.html: http://blog.csdn.net/zhoubo616 ...
- 安装node/npm,通过express搭建node项目
nodejs软件的下载地址:https://nodejs.org/en/ (推荐下载稳定版) 1.只要安装好了nodejs,就自动安装好了npm包. 2.在cmd中通过命令node -version查 ...
- 作业-[luogu4396][AHOI2013]-莫队
<题面> 卡常终究比不上算法的优化…… 这是莫队的有点小坑的题, 首先不一定能想到,想到不一定打对,打对不一定打好. 首先你会发现,这个题的时限是很长的- $n$和$m$也是很大的. 于是 ...
- hdu oj 1520 Anniversary party(树形dp入门)
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Python 文件读写小结
- 洛谷P1312 [NOIP2011提高组Day1T3]Mayan游戏
Mayan游戏 题目描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游 ...
- Hdu 1150
Machine Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- ASP.NET自定义控件组件开发 第一章 第一章:从一个简单的控件谈起
第一章:从一个简单的控件谈起 系列文章链接: ASP.NET自定义控件组件开发 第一章 待续 ASP.NET自定义控件组件开发 第一章 第二篇 接着待续 ASP.NET自定义控件组件开发 第一章 第三 ...
- Spring Boot → 09:使用外置Servlet容器_tomcat9.0
Spring Boot → 09:使用外置Servlet容器_tomcat9.0