Codeforces 429B - Working out (DP)
2016-03-07 19:28
513 查看
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.
Examples
Input
Output
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].
这道题需注意所走路线不能重合;
要去枚举所有相遇的点;
需要开四个数组表示在i,j相遇的最大值,分别是从
a[1][1]-->a[n,m];
b
[1]-->b[1][
c[1][m]-->c
[
d
[m]-->d[1][1];
eg.
假设在(2,2)处相遇
case 1:
case 2:
比较这两种情况的最大值并记录下来.
输出所有相遇点中最大的即可.
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.
Examples
Input
3 3 100 100 100 100 1 100 100 100 100
Output
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].
这道题需注意所走路线不能重合;
要去枚举所有相遇的点;
需要开四个数组表示在i,j相遇的最大值,分别是从
a[1][1]-->a[n,m];
b
[1]-->b[1][
c[1][m]-->c
[
d
[m]-->d[1][1];
eg.
假设在(2,2)处相遇
case 1:
a (in) | |||
b (in) | meet | b (out) | |
a (out) | |||
b (out) | |||
a (in) | meet | a (out) | |
b (in) | |||
输出所有相遇点中最大的即可.
#include<stdio.h> #include<algorithm> using namespace std; int a[1005][1005]; int b[1005][1005]; int c[1005][1005]; int d[1005][1005]; int e[1005][1005]; int f[1000005]; int main() { int n,m; scanf("%d%d",&n,&m); for (int i=0;i<=n+1;i++) a[i][0]=0; for (int j=0;j<=m+1;j++) a[0][j]=0; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { scanf("%d",&a[i][j]); } for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) { b[i][j]=max(b[i-1][j]+a[i][j],b[i][j-1]+a[i][j]); } for (int i=n;i>=1;i--) for (int j=1;j<=m;j++) { c[i][j]=max(c[i+1][j]+a[i][j],c[i][j-1]+a[i][j]); } for (int i=1;i<=n;i++) for (int j=m;j>=1;j--) { d[i][j]=max(d[i][j+1]+a[i][j],d[i-1][j]+a[i][j]); } for (int i=n;i>=1;i--) for (int j=m;j>=1;j--) { e[i][j]=max(e[i+1][j]+a[i][j],e[i][j+1]+a[i][j]); } int coutt=0; for (int i=2;i<=n-1;i++) for (int j=2;j<=m-1;j++) { if ((b[i-1][j]+e[i+1][j]+c[i][j-1]+d[i][j+1])>(b[i][j-1]+e[i][j+1]+c[i+1][j]+d[i-1][j])) f[coutt]=b[i-1][j]+e[i+1][j]+c[i][j-1]+d[i][j+1]; else f[coutt]=b[i][j-1]+e[i][j+1]+c[i+1][j]+d[i-1][j]; coutt++; } int ans=-1; for (int i=0;i<coutt;i++) { if (f[i]>ans) ans=f[i]; } printf("%d\n",ans); }
相关文章推荐
- 混合语言编程:启用CLR(公共语言运行时编译)让C#调用C++
- 课程不明白的问题?
- 【转载】面试干货!21个必知数据科学面试题和答案
- 设计模式之单例模式饿汉式
- 一些奇怪的东西
- 深度优先遍历的栈实现
- hdu 3962(AC自动机+矩阵优化dp)
- 目前流行的源程序版本管理软件和项目管理软件有哪些,各有什么缺点?
- QtCreator-----快捷键
- ACM freopen
- 工作流的项目 activity-login-context.xml Failed to read schema document
- iOS开发拓展篇—CoreLocation定位服务
- 2016台式机攒机
- Linux下查看客户端和服务端端口占用情况
- Oracle SPM(SQL Plan Management)介绍及演示SQL
- new................未完待续
- 《ZeroC Ice 权威指南》笔记
- http 的八种方法(动作)
- HDU5637 bestcoder#74 Transform 题解&代码
- ZeroC Ice 权威指南 - leader us 笔记