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 ...
随机推荐
- 大数据阶乘(The factorial of large data)
题目描述 Description 阶乘是计算中的基础运算,虽然规则简单,但是位数太多了,也难免会出错.现在的问题是:给定任意位数(long long类型)的一个数,求它的阶乘,请给出正确结果.为提高速 ...
- jvm的可见性的理解
同步包括两方面的含义: 独占性和可见性. 很多人仅仅理解了独占性,而忽略了可见性. 根据Java Language Specification中的说明, jvm系统中存在一个主内存(Main Memo ...
- ( ̄y▽ ̄)~ 智能手机II
( ̄y▽ ̄)~ 智能手机II TimeLimit: 3000/1000 MS (Java/Others) MenoryLimit: 32768/32768 K (Java/Others) 64-bi ...
- js控制日期选择框datetime-local和select的展开
注: js控制元素展开不受元素css属性的限制,例如opacity,z-index等 1. 使用js控制日期选择框的展开 ios: document.querySelector(".targ ...
- Apriori算法-数组-C语言
原文地址:http://blog.csdn.net/liema2000/article/details/6118423 #include<stdio.h>typedef struct { ...
- Chapter 1 First Sight——34
"Was that the boy I sat next to in Biology?" I asked artlessly. 你是生物课坐在我旁边的男生吗?我天真烂漫的问道. & ...
- VMWare桥接、NAT和only-host三种模式
如果你想利用VMWare安装虚拟机,或想创建一个与网内其他机器相隔离的虚拟系统,进行特殊的调试工作.此时,对虚拟系统网络连接模式的选择就非常重要了.如果你选择的工作模式不正确,就无法实现上述目的,也就 ...
- ieee80211_rx
ieee80211rx.c(E:\code\linux\net\ieee80211) 所有接收到的帧都送到这个函数中去 int ieee80211_rx(struct ieee80211_device ...
- HDU2629:Identity Card
Problem Description Do you own an ID card?You must have a identity card number in your family's Hous ...
- 使用 Flex 库项目---打包swc
来源:http://help.adobe.com/zh_CN/flashbuilder/using/WSe4e4b720da9dedb5-1a92eab212e75b9d8b2-7ffe.html ...