CodeForces 429 B B. Working out
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 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.
Sample Input
Input Output Hint
Iahub will choose exercises a[][] → a[][] → a[][] → a[][] → a[][]. Iahubina will choose exercises a[][] → a[][] → a[][] → a[][] → a[][].
题目链接:http://codeforces.com/problemset/problem/429/B
*********************************************************
题意: Iahub 和 Iahubina在一个矩阵健身房锻炼身体,Iahub从(1,1)出发,要到达(n,m);Iahubina从(n,1)出发,要到达(1,m);他们俩还必须要见上一面
解题思路:可以枚举每个可能相遇的点所产生的最大情况
dp1[i][j] 从(1,1)到(i,j)的最大值
dp2[i][j] 从(n,m)到(i,j)的最大值
dp3[i][j] 从(n,1)到(i,j)的最大值
dp1[i][j] 从(1,m)到(i,j)的最大值 ans为最终结果:
由于遍历的每个点是,可以分为四个部分
(1,1)->(i,j)+(i,j)->(n,m) + (n,1)->(i,j) + (1,m)->(i,j)
因此可以用ans更新四部分和的最大值 AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std; #define N 1200
#define INF 0x3f3f3f3f int maps[N][N],dp1[N][N],dp2[N][N],dp3[N][N],dp4[N][N]; int main()
{
int n,m,i,j; while(scanf("%d %d", &n,&m) != EOF)
{
memset(dp1,,sizeof(dp1));
memset(dp2,,sizeof(dp2));
memset(dp3,,sizeof(dp3));
memset(dp4,,sizeof(dp4));
memset(maps,,sizeof(maps)); for(i=; i<=n; i++)
for(j=; j<=m; j++)
scanf("%d", &maps[i][j]); for(i=; i<=n; i++)///(1,1)到(i,j)最大值
for(j=; j<=m; j++)
dp1[i][j]=max(dp1[i-][j],dp1[i][j-])+maps[i][j]; for(i=n; i>=; i--)///(n,m)到(i,j)最大值
for(j=m; j>=; j--)
dp2[i][j]=max(dp2[i+][j],dp2[i][j+])+maps[i][j]; for(i=n; i>=; i--)///(n,1)到(i,j)最大值
for(j=; j<=m; j++)
dp3[i][j]=max(dp3[i][j-],dp3[i+][j])+maps[i][j]; for(i=; i<=n; i++)///(1,m)到(i,j)最大值
for(j=m; j>=; j--)
dp4[i][j]=max(dp4[i][j+],dp4[i-][j])+maps[i][j]; int ans=;
for(i=; i<n; i++)
for(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+]);
}///(1,1)->(i,j)+(i,j)->(n,m) + (n,1)->(i,j) + (1,m)->(i,j) printf("%d\n", ans);
}
return ;
}
CodeForces 429 B B. Working out的更多相关文章
- Codeforces 429 B. Working out-dp( Codeforces Round #245 (Div. 1))
B. Working out time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- codeforces 429 On the Bench dp+排列组合 限制相邻元素,求合法序列数。
限制相邻元素,求合法序列数. /** 题目:On the Bench 链接:http://codeforces.com/problemset/problem/840/C 题意:求相邻的元素相乘不为平方 ...
- CodeForces 429 B Working out(递推dp)
题目连接:B. Working out 我想了很久都没有想到怎么递推,看了题解后试着自己写,结果第二组数据就 wa 了,后来才知道自己没有判选择的两条路径是否只是一个交点. 大概思路是:先预处理出每个 ...
- Codeforces 429 A. Xor-tree
下来的第一次相遇是在不翻盖的同一节点,递归可以是.... A. Xor-tree time limit per test 1 second memory limit per test 256 mega ...
- CodeForces 840C - On the Bench | Codeforces Round #429 (Div. 1)
思路来自FXXL中的某个链接 /* CodeForces 840C - On the Bench [ DP ] | Codeforces Round #429 (Div. 1) 题意: 给出一个数组, ...
- CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)
思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...
- codeforces Round#429 (Div2)
2017-08-20 10:00:37 writer:pprp 用头文件#include <bits/stdc++.h>很方便 A. Generous Kefa codeforces 84 ...
- 【Codeforces Round #429 (Div. 2) A】Generous Kefa
[Link]:http://codeforces.com/contest/841/problem/A [Description] [Solution] 模拟,贪心,每个朋友尽量地多给气球. [Numb ...
随机推荐
- ArrayList和LinkedList和Vector源码分析
ArrayList源码: private static final int DEFAULT_CAPACITY = 10;//默认长度 /** * Shared empty array instance ...
- css实现多行超出显示省略号?
可以实现,但是用的是-webkit-私有属性.我用js已经解决了.代码如下:text-overflow: -o-ellipsis-lastline;overflow: hidden;text-over ...
- VI/VIM 常用命令
VI/VIM 常用命令=========== 整理自鸟哥的私房菜 ---------- - 移动光标 命令 | 描述----------------------- ...
- Javascript:scrollWidth,clientWidth,offsetWidth的区别(转)
网页可见区域宽:document.body.clientWidth; 网页可见区域高:document.body.clientHeight; 网页可见区域高:document.body.offsetW ...
- nginx 实现Web应用程序的负载均衡
文章转载自 博客园, 原文地址 http://www.cnblogs.com/ivanyb/archive/2011/11/16/2250710.html 看到园子中的大牛代震军写的一篇玩玩负载均衡- ...
- Chapter 1 First Sight——30
The girl sitting there giggled. I'd noticed that his eyes were black — coal black. 那个坐在那里的女孩笑着.我注意到她 ...
- 第四十二节,configparser特定格式的ini配置文件模块
configparser用于处理特定格式的文件,其本质上是利用open来操作文件. 特定格式的ini配置文件模块,用于处理ini配置文件,注意:这个ini配置文件,只是ini文件名称的文本文件,不是后 ...
- JavaScript shift() 方法
http://www.w3school.com.cn/jsref/jsref_shift.asp 定义和用法 shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元素的值. 语法 arr ...
- debian下安装repo
1.去google网站上下载repo脚本(用php语言写成的脚本) https://gerrit.googlesource.com/git-repo/+/stable/repo 可以将脚本复制下来并保 ...
- E212:无法打开并写入文件
用vi编辑文件是 老师出现这样的错误 有些文件 需要root权限才能修改 切换成root权限就行了