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)如果需 ...
随机推荐
- Android Studio 解决Fetching android sdk component information加载过久问题
extends:http://www.cnblogs.com/sonyi/p/4154797.html 安装完成后,如果直接启动,Android Studio会去获取 android sdk 组件信息 ...
- eclipse启动报错 Problems occurred when invoking code from plug-in: "org.eclipse.jface"
eclipse在使用中可能会发生错误: Problems occurred when invoking code from plug-in: "org.eclipse.jface" ...
- dubbo有什么作用
转自:http://blog.csdn.net/ichsonx/article/details/39008519 1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的R ...
- Dcloud开发-- 打开蓝牙
这样打开APP就会直接提示是否要打开蓝牙: <script type="text/javascript"> mui.init(); mui.plusReady(func ...
- postgresql----索引失效
什么是索引失效?如果where过滤条件设置不合理,即使索引存在,且where过滤条件中包含索引列,也会导致全表扫描,索引不起作用.什么条件下会导致索引失效呢? 1.任何计算.函数.类型转换 2.!= ...
- flask中Flask()和Blueprint() flask中的g、add_url_rule、send_from_directory、static_url_path、static_folder的用法
1.Blueprint()在蓝本注册函数register_blueprint()中,第一个参数为所注册的蓝本名称.当我们在应用对象上注册一个蓝图时,需要指定一个url_prefix关键字 参数(这个参 ...
- Network---3694poj(桥与LCA)
题目链接 题意: 有n个电脑1-n,m个连接,由于可能存在一些桥,如果这些桥出现了问题,那么会导致一些电脑之间无法连接, 所以建立链接Q次,每次链接a和b电脑,求链接ab后还存在几个桥: 如果 ...
- Python并行编程(十三):进程池和mpi4py模块
1.基本概念 多进程库提供了Pool类来实现简单的多进程任务.Pool类有以下方法: - apply():直到得到结果之前一直阻塞. - apply_async():这是apply()方法的一个变体, ...
- 解决Eclipse中新建jsp文件总是以ISO8859-1编码问题
eclipse --> window -->Preferences-->web-->jsp-->utf-8
- 在SQL Server里如何进行数据页级别的恢复
在SQL Server里如何进行页级别的恢复 关键词:数据页修复 在今天的文章里我想谈下每个DBA应该知道的一个重要话题:在SQL Server里如何进行页级别还原操作.假设在SQL Server里你 ...