Codeforces Round #245 (Div. 1) B. Working out dp
题目链接:
http://codeforces.com/contest/429/problem/B
B. Working out
time limit per test2 secondsmemory limit per test256 megabytes
#### 问题描述
> 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.
#### 输入
> 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).
#### 输出
> The output contains a single number — the maximum total gain possible.
#### 样例
> **sample input**
> 3 3
> 100 100 100
> 100 1 100
> 100 100 100
>
> **sample output**
> 800
题意
每个点有a[i][j]的物品(非负数),现在有一个人要从(1,1)到(n,m),且只能往下或者往右走,另一个人从(n,1)->(1,m),且只能往上或往右走,现在要让这两个人的路线有且只有一个公共点(且公共点的物品谁都不能取),问如何规划使得两个人能够获得的物品总数最多。
题解
这题比较特殊的地方在公共点,从公共点(x,y)出发,你会发现我们把原问题划分成了四个简单的子问题:(1,1)->(x,y),(n,1)->(x,y),(n,m)->(x,y),(1,m)->(x,y),但是你发现枚举这个点是不够的,还需要枚举这个点的上下左右四个点(既如何安排这两个人进入x,y的入口,使得刚好能够只有(x,y)这一个公共点)
代码
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;
const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);
//start----------------------------------------------------------------------
const int maxn=1e3+10;
int arr[maxn][maxn];
int dp[4][maxn][maxn];
int n,m;
void init(){
clr(dp,0);
}
int main() {
scf("%d%d",&n,&m);
init();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scf("%d",&arr[i][j]);
}
}
//(1,1)->(n,m)
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
dp[0][i][j]=max(dp[0][i-1][j],dp[0][i][j-1])+arr[i][j];
}
}
//(n,1)->(1,m)
for(int i=n;i>=1;i--){
for(int j=1;j<=m;j++){
dp[1][i][j]=max(dp[1][i][j-1],dp[1][i+1][j])+arr[i][j];
}
}
//(n,m)->(1,1)
for(int i=n;i>=1;i--){
for(int j=m;j>=1;j--){
dp[2][i][j]=max(dp[2][i+1][j],dp[2][i][j+1])+arr[i][j];
}
}
//(1,m)->(n,1)
for(int i=1;i<=n;i++){
for(int j=m;j>=1;j--){
dp[3][i][j]=max(dp[3][i-1][j],dp[3][i][j+1])+arr[i][j];
}
}
int ans=-1;
//枚举相遇点
for(int i=2;i<n;i++){
for(int j=2;j<m;j++){
ans=max(ans,dp[0][i-1][j]+dp[2][i+1][j]+dp[1][i][j-1]+dp[3][i][j+1]);
ans=max(ans,dp[0][i][j-1]+dp[2][i][j+1]+dp[1][i+1][j]+dp[3][i-1][j]);
}
}
printf("%d\n",ans);
return 0;
}
//end-----------------------------------------------------------------------
Codeforces Round #245 (Div. 1) B. Working out dp的更多相关文章
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- Codeforces Round #245 (Div. 1) 429D - Tricky Function 最近点对
D. Tricky Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/42 ...
- Codeforces Round #245 (Div. 1) B. Working out (简单DP)
题目链接:http://codeforces.com/problemset/problem/429/B 给你一个矩阵,一个人从(1, 1) ->(n, m),只能向下或者向右: 一个人从(n, ...
- Codeforces Round #245 (Div. 1) B. Working out (dp)
题目:http://codeforces.com/problemset/problem/429/B 第一个人初始位置在(1,1),他必须走到(n,m)只能往下或者往右 第二个人初始位置在(n,1),他 ...
- Codeforces Round #245 (Div. 2) C. Xor-tree DFS
C. Xor-tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem/C ...
- Codeforces Round #245 (Div. 2) B. Balls Game 并查集
B. Balls Game Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/430/problem ...
- Codeforces Round #245 (Div. 2) A. Points and Segments (easy) 贪心
A. Points and Segments (easy) Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...
- 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 Round #245 (Div. 2) B - Balls Game
暴利搜索即可 #include <iostream> #include <vector> #include <iostream> using namespace s ...
随机推荐
- C语言写的2048小游戏
基于"基于C_语言的2048算法设计_颜冠鹏.pdf" 这一篇文献提供的思路 在中国知网上能找到 就不贴具体内容了 [摘 要] 针对2048的游戏规则,分析了该游戏的算法特点,对其 ...
- [转]windows下多个python版本共存,pip使用
windows下多个python版本共存,pip使用 2017年09月13日 17:21:30 阅读数:2574 一.同时装了Python3和Python2,怎么区分 了解python的人都知道pyt ...
- Home Assistant系列 -- 接入手机摄像头做实时监控和人脸识别
准备一部废旧(土豪忽略,主要是穷)的.摄像头还是好的手机做监控设备,(Android 和iPhone都行)当Home Assistant 获得实时的视频流后,可以接入各种图像处理组件完成人脸识别,动作 ...
- jenkins+maven+docker集成java发布(二)#远程发布
jenkins+maven+docker集成java发布(一)中写了在Jenkins服务器自动部署业务,那需要将java项目部署到其他服务器怎么操作 这里需要依赖插件Publish Over SSH ...
- Leetcode---栈系列刷题(python3实现)----#496 下一个更大元素I
给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集.找到 nums1 中每个元素在 nums2 中的下一个比其大的值. nums1 中数字 x 的下一个更 ...
- homebrew 使用心得
''' 安装anaconda 安装命令: brew search anaconda brew cask install anaconda 添加环境变量: vi ~/.bash_profile expo ...
- 最短寻道优先算法(SSTF)——磁盘调度管理
原创 最近操作系统实习,敲了实现最短寻道优先(SSTF)——磁盘调度管理的代码. 题目阐述如下: 设计五:磁盘调度管理 设计目的: 加深对请求磁盘调度管理实现原理的理解,掌握磁盘调度算法. 设计内容: ...
- 20155320 2016-2017-2《Java程序设计》第九周学习总结
20155320 2016-2017-2<Java程序设计>第九周学习总结 教材学习内容总结 第十六章 JDBC简介 撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 ...
- 20155334 2016-2017-2 《Java程序设计》第一周学习总结
20155334 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 第一章主要讲了Java的前世今生以及Java的三大平台(Java SE.Java EE.Ja ...
- 自己在UWP程序上调用usb转串口的路程
之前一直是在普通的framework环境下写串口,使用的类为 SerialPort 这个类大家可能比较熟悉.但是在UWP的开发里,使用的是 SerialDevice 这个类,是不一样的. 1. 清单文 ...