Working out - b(dp之路)
来源:互联网 发布:软件人才外包公司 编辑:程序博客网 时间:2024/05/29 18:05
B. Working out
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 matrixa with n lines andm 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 column1. 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 workouta[n][1] and she needs to finish with workouta[1][m]. After finishing workout from cella[i][j], she goes to eithera[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.
The first line of the input contains two integers n andm (3 ≤ n, m ≤ 1000). Each of the nextn lines contains m integers:j-th number from i-th line denotes elementa[i][j] (0 ≤ a[i][j] ≤ 105).
The output contains a single number — the maximum total gain possible.
3 3100 100 100100 1 100100 100 100
800
Iahub will choose exercises a[1][1] → a[1][2] → a[2][2] → a[3][2] → a[3][3]. Iahubina will choose exercisesa[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].
四个角递推dp
#include <stdio.h>#include <string.h>#include <iostream>using namespace std;const int inf=0X3f3f3f3f;int dp[5][1010][1010], ma[1010][1010];int main(){ int i, j, n, m; while(~scanf("%d%d", &n, &m)) { for(i = 1; i <= n; i++) for(j = 1; j <= m; j++) { scanf("%d", &ma[i][j]); } memset(dp, 0, sizeof(dp)); for(i = 1; i <= n; i++) { for(j = 1; j <= m; j++) { dp[1][i][j] = max(dp[1][i-1][j], dp[1][i][j-1]) + ma[i][j]; } for(j = m; j >= 1; j--) { dp[3][i][j] = max(dp[3][i-1][j], dp[3][i][j+1]) + ma[i][j]; } } for(i = n; i >= 1; i--) { for(j = 1; j <= m; j++) { dp[2][i][j] = max(dp[2][i+1][j], dp[2][i][j-1]) + ma[i][j]; } for(j = m; j >= 1; j--) { dp[4][i][j] = max(dp[4][i+1][j], dp[4][i][j+1]) + ma[i][j]; } } for(i = 0; i <= n+1; i++) { for(j = 1; j <= 4; j++) { dp[j][i][0] = dp[j][i][m+1] = -inf; } } for(i = 0; i <= m+1; i++) { for(j = 1; j <= 4; j++) { dp[j][0][i] = dp[j][n+1][i] = -inf; } } __int64 ans = -inf; for(i = 1; i <= n; i++) { for(j = 1; j <= m; j++) { long long temp=dp[1][i][j-1]+dp[4][i][j+1]+dp[2][i+1][j]+dp[3][i-1][j]; ans=max(ans,temp); temp=dp[1][i-1][j]+dp[4][i+1][j]+dp[2][i][j-1]+dp[3][i][j+1]; ans=max(ans,temp); } } printf("%I64d\n", ans); } return 0;}
- Working out - b(dp之路)
- B. Working out (递推dp )
- B. Working out----暴力dp
- Codeforces 429B B. Working out (DP)
- [dp] Codeforces 429B B. Working out
- codeforces 429B B. Working out(dp)
- dp Codeforces 429B B. Working out
- Codeforces 429B B. Working out dp
- Codeforces Round #245 (Div. 1) -- B. Working out (DP)
- Codeforces Round #245 (Div. 1) B. Working out(DP)
- Codeforces Round #245 (Div. 1) -- B. Working out (DP)
- cf 429B Working out dp
- Codeforces 429B - Working out (DP)
- Codeforces#245- B. Working out-DP
- CF 429B. Working out DP
- CodeForces - 429B Working out(dp)
- CF B. Working out dp 递推
- codeforces429B working out(dp)
- 清橙OJ A1212. 剪枝
- 一个不错的redis gui客户端
- 数据结构上机测试2-1:单链表操作A
- 解析ajax核心XMLHTTPRequest对象的创建与浏览器的兼容问题
- C# 鼠标事件
- Working out - b(dp之路)
- 从左到右将二进制数转化为十进制数
- vue2.0 router遇到的问题
- ASP.NET AJAX Advance Tips & Tricks (9) DropDownList在Firefox下的奇怪现象和解决方案——谁的BUG?
- 百度云服务器搭建(一)
- ASP.NET AJAX Advance Tips & Tricks (10) 解决使用AJAX Extender时的页面导出(Word/Excel)问题(Extender control 'XXX'
- vsftpd服务自启动的三种方法
- ASP.NET AJAX Advance Tips & Tricks (11) 三种方法动态创建Tooltip
- 数据结构之 树的储存和遍历总结