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)如果需 ...
随机推荐
- ubuntu 创建桌面快捷方式
$sudo apt-get install gnome-panel $gnome-desktop-item-edit /home/xxx/桌面 --create-new 命令行:填入程序名称,如/u ...
- handlebars的使用
web 开发中,js 解析JSON 是经常的事情.非常繁琐.handlebars 使用了模版,只要你定义一个模版,提供一个json对象,handlebars 就能吧json对象放到你定的模版中 htm ...
- 【BZOJ2938】[Poi2000]病毒 AC自动机+DFS
[BZOJ2938][Poi2000]病毒 Description 二进制病毒审查委员会最近发现了如下的规律:某些确定的二进制串是病毒的代码.如果某段代码中不存在任何一段病毒代码,那么我们就称这段代码 ...
- 一个Activity中使用两个layout实例
package com.sbs.aas2l; import android.app.Activity; import android.os.Bundle; import android.view.Vi ...
- jenkins之jenkins与gitlab集成
实现当git代码使用push的时候自动构建 安装gitlab钩子插件 安装令牌认证插件 在主机上面生产token(其实随便弄一个简单的字符串也可以) openssl rand -hex 10 修改任务 ...
- 徐州网络赛F-Feature Trace【暴力】
Morgana is learning computer vision, and he likes cats, too. One day he wants to find the cat moveme ...
- D. Little Artem and Dance---cf669D(模拟)
题目链接:http://codeforces.com/problemset/problem/669/D 给你n个数,一开始是1 2 3 4 5 6 ... n 这样的 现在有两个操作,第一个操作是所有 ...
- mysql 数据操作 单表查询 where约束 is null in
需求找出年龄是 81 或者 73 或者 28 mysql ; +----+-----------+--------+-----+------------+-----------+----------- ...
- centos tomcat/resin安装配置 卸载系统自带的java tomcat安装配置 安装JDK resin安装配置 第二十八节课
centos tomcat/resin安装配置 卸载系统自带的java tomcat安装配置 安装JDK resin安装配置 第二十八节课 tomcat和java都不需要编译 tom ...
- go-003-基础语法
1.行分隔符 一行代表一个语句结束. 如果一行多个,使用“;”分割,不推荐使用,建议使用默认一行一个语句 2.标识符 标识符用来命名变量.类型等程序实体.一个标识符实际上就是一个或是多个字母(A~Z和 ...