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 ...
随机推荐
- Redis使用:聚合类型为空时,会自动被Redis删除
项目中使用Redis来记录用户的上线和下线信息,其中用到了集合(sets)类型,某用户上线时,向sets中添加数据,下线时将相应数据从sets中删除,考虑当该用户的所有实例都下线时,需要将sets删除 ...
- [Array]448. Find All Numbers Disappeared in an Array
Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...
- 移动端 Iphone拍照变横问题的解决
在移动端的页面需要做用户拍照上传的功能时会有用,苹果即使竖着拍照,上传到网页后它也会变成横的,好像IOS得一个BUG,安卓就没有这个问题. 要解决这个问题需要引入exif.js这个库,网上随便搜一下就 ...
- sass进阶
代码的重用 基础的部分我们讲述了变量 Mixin 这两种方法可以增加扩展和重用 现在开始继续学习:extend继承 .class1 { border: 1px solid #ddd; } .class ...
- 20190722-Moni和Boly的故事
Moni(模拟)可以得到60分 Boly(暴力)可以得到40分 还好说,这次有点可行. 过程是: 先看了T3,可以模拟,然后做T1T2 T1 好说,$exgcd$,不会. 暴力,暴力!骗了40. T2 ...
- https方式nginx 代理tomcat访问不带www的域名301重定向跳转到www的域名帮助seo集中权重
比如我要把 http://gucanhui.com http://www.gucanhui.com 跳转到https://www.gucanhui.com 用F12的network可以看到状态码301 ...
- MySQL错误1055
问题描述:在MySQL数据库下,执行SQL插入语句报错.错误信息如下: 错误原因:在MySQL5.7之后,sql_mode中默认存在ONLY_FULL_GROUP_BY,SQL语句未通过ONLY_FU ...
- 统计py文件或目录代码行数
bug:当遇到3个"""时 可能会将下面的代码不计入代码总行数 import os def count_path(path,countcode): if os.path. ...
- Django REST Framework之分页器
Django REST Framework提供了三种分页器: PageNumberPagination.基于Django Paginator封装,使得操作更方便,只需要做一些配置即可.分页方式:根据页 ...
- (转)Json在Unity中的简单使用
Json数据解析在Unity3d中的应用 最近做项目过程中因为Json文件名写错了一个字母Unity报错,找错误找到半夜,当时为了验错,写了一个小Demo,正好借此总结一下Json. 1.什么是Jso ...