cf 429 B Working out
2 seconds
256 megabytes
standard input
standard output
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.
3 3
100 100 100
100 1 100
100 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 exercises a[3][1] → a[2][1] → a[2][2] → a[2][3] → a[1][3].
题目意思:
给n*m的矩阵,每个格子有个数,A从(1,1)出发只能向下或右走,终点为(n,m),B从(n,1)出发只能向上或右走,终点为(1,m)。两个人的速度不一样,走到的格子可以获的该格子的数,两人相遇的格子上的数两个人都不能拿。求A和B能拿到的数的总和的最大值。
n,m<=1000
解题思路:
dp.
先预处理出每个格子到四个角落格子的路径最大数值,然后枚举两个人相遇的交点格子,枚举A、B的进来和出去方式,求最大值即可。
注意边界情况。
#include<iostream>
#include<string.h>
#include<string>
#include<cmath>
using namespace std; typedef long long ll;
const int N = 1e3+;
int n,m;
ll f1[N][N], f2[N][N], f3[N][N], f4[N][N];
ll a[N][N]; ll getRes(int i,int j) {
return max( f1[i][j-] + f4[i][j+] + f2[i+][j] + f3[i-][j]
, f1[i-][j] + f4[i+][j] + f2[i][j-] + f3[i][j+]);
} int main () {
//freopen("./Desktop/in.txt","r",stdin);
scanf("%d %d", &n, &m);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
cin >> a[i][j]; for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
f1[i][j] = max(f1[i-][j], f1[i][j-]) + a[i][j];
for(int i=n;i>=;i--)
for(int j=;j<=m;j++)
f2[i][j] = max(f2[i][j-], f2[i+][j]) + a[i][j];
for(int i=;i<=n;i++)
for(int j=m;j>=;j--)
f3[i][j] = max(f3[i-][j], f3[i][j+]) + a[i][j];
for(int i=n;i>=;i--)
for(int j=m;j>=;j--)
f4[i][j] = max(f4[i+][j], f4[i][j+]) + a[i][j];
ll mx=;
for(int i=;i<n;i++) {
for(int j=;j<m;j++) {
ll res = getRes(i,j);
mx = max(mx, res);
//printf("%lld ",res);
}//puts("");
}
cout << mx <<endl;
return ;
}
cf 429 B Working out的更多相关文章
- 【CF Round 429 B. Godsend】
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- CF memsql Start[c]UP 2.0 A
CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...
- CF memsql Start[c]UP 2.0 B
CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
随机推荐
- nexus 增加代理仓库 无法搜到snapshot的jar包 解决方法
如题, nexus 私服 增加了另一个 私服, 但是无法搜到 版本中带有 snapshot字样的 jar包. 环境情况: 1.老私服: 首先版本中带有 snapshot字样的 jar包,是发布在 老 ...
- js+jquery
改变元素的不透明度 <!DOCTYPE html> <html> <head> <script> function ChangeOpacity(x) { ...
- Windows Phone 7 程序等待页面的处理
程序启动通常会有一个等待的过程,在这个过程中可以通过使用Popup控件配合BackgroundWorker类启动后台线程来实现. 控件的代码 PopupSplash.xaml <UserCont ...
- thinkphp---手机访问切换模板!
手机访问切换模板:一般用在手机在做自适应的情况. 第一步:需要添加判断是否是手机访问的方法: http://www.cnblogs.com/e0yu/p/7561811.html 第二步:Home / ...
- 170525、解决maven隐式依赖包版本问题
今天在使用dubbo2.5.3版本的时候,启动项目的时候发现一个问题,tomcat启动一直报错 Caused by: java.lang.IllegalStateException: Context ...
- Shell--基础知识
变量的定义: a=1 b=hello c="hello world !" d='hello "反启" !' e=`ls` (注意:这是反引号) 备注:=号左右 ...
- proxy ubuntu proxy--http://jingyan.baidu.com/article/8cdccae9913470315513cd70.html
apt-get 设置代理 proxy 方法 方法一 :这是一种临时的手段,如果你仅仅是暂时需要通过http代理使用apt-get,你可以使用这种方法. 在使用 apt-get 之前,在终端中输入以下 ...
- 广通软件获“2016年度中国最具影响力IT运维管理软件提供商”殊荣
12月16日,“科技原力觉醒引领创新巅峰”-- 2016创新影响力年会暨国家产业服务平台•2016年终评活动在北京裕龙国际酒店落下帷幕. 本活动在主管部门的指导参与下,总结本年度技术成果并籍此对未来科 ...
- 符合语言习惯的 Python 优雅编程技巧
Python最大的优点之一就是语法简洁,好的代码就像伪代码一样,干净.整洁.一目了然.要写出 Pythonic(优雅的.地道的.整洁的)代码,需要多看多学大牛们写的代码,github 上有很多非常优秀 ...
- editplus的常用快捷键
小编给大家整理了一些软件的快捷键.http://www.downza.cn/soft/187814.html 创建当前行的副本:Ctrl+J 反转选定文本的大小写:Ctrl+K 选择当前行:Ctrl+ ...