cf 429B Working out(简单dp)
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从左上角走到右下角,B从左下角走到右上角,路线交叉处的权值不算,问两条路线权值之和最大值。要求:两条路线只在一点交叉。
可以枚举交叉点,求该点到四个角落的权值之和。到每个角的权值都可以dp得到最大值。从左上角顺时针得到0,1,2,3四个方向。
两幅图的权值之和分别是: dp[i][j-1][0] + dp[i-1][j][1] + dp[i][j+1][2] + dp[i+1][j][3]
dp[i][j-1][3] + dp[i-1][j][0] + dp[i][j+1][1] + dp[i+1][j][2]
还有需注意的是,交叉点只在(n-2)*(m-2)里面这个矩形里变化(否则一个角上的矩形会不存在)
- #include <bits/stdc++.h>
- using namespace std;
- const int MAXN = ;
- __int64 a[MAXN][MAXN];
- __int64 dp[MAXN][MAXN][];//0, 1, 2, 3分别代表左上,右上,右下,左下
- int main()
- {
- int n, m;
- int i, j;
- while (~scanf("%d%d", &n, &m)) {
- for (i = ; i <= n; ++i) {
- for (j = ; j <= m; ++j) {
- scanf("%I64d", &a[i][j]);
- }
- }
- memset(dp, , sizeof(dp));
- for (i = ; i <= n; ++i) {
- for (j = ; j <= m; ++j) {
- dp[i][j][] = max(dp[i - ][j][], dp[i][j - ][]) + a[i][j];
- }
- }
- for (i = ; i <= n; ++i) {
- for (j = m; j >= ; --j) {
- dp[i][j][] = max(dp[i - ][j][], dp[i][j + ][]) + a[i][j];
- }
- }
- for (i = n; i >= ; --i) {
- for (j = m; j >= ; --j) {
- dp[i][j][] = max(dp[i + ][j][], dp[i][j + ][]) + a[i][j];
- }
- }
- for (i = n; i >= ; --i) {
- for (j = ; j <= m; ++j) {
- dp[i][j][] = max(dp[i + ][j][], dp[i][j - ][]) + a[i][j];
- }
- }
- __int64 ans = ;
- for (i = ; i < n; ++i) {
- for (j = ; j < m; ++j) {
- ans = max(ans, dp[i][j - ][] + dp[i - ][j][] + dp[i][j + ][] + dp[i + ][j][]);
- ans = max(ans, dp[i][j - ][] + dp[i - ][j][] + dp[i][j + ][] + dp[i + ][j][]);
- }
- }
- printf("%I64d\n", ans);
- }
- return ;
- }
cf 429B Working out(简单dp)的更多相关文章
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- codeforces Gym 100500H A. Potion of Immortality 简单DP
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- hdu1087 简单DP
I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
- poj 1157 LITTLE SHOP_简单dp
题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...
- hdu 2471 简单DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 简单dp, dp[n][m] +=( dp[n-1][m],dp[n][m-1],d[i][k ...
- Codeforces 41D Pawn 简单dp
题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...
- poj1189 简单dp
http://poj.org/problem?id=1189 Description 有一个三角形木板,竖直立放.上面钉着n(n+1)/2颗钉子,还有(n+1)个格子(当n=5时如图1).每颗钉子和周 ...
随机推荐
- linux recv函数返回值分析
函数原型: ssize_t recv(int sockfd, void *buf, size_t len, int flags); 该函数第一个参数制定接收端套接字描述符; 第二个参数指明一个缓冲区, ...
- 《Lucene in Action第二版》学习总结---如何在Windows下编译luceneinAction2Edition源码包
1. 下载此源码包,位置是: www.manning.com/hatcher3,得到:lia2e.tar.gz,然后解压缩得到目录:lia2e,为了以后能辨识,我将此目录改名为:luceneinAct ...
- php判断页面是否是微信打开的示例(微信打开网页)
代码如下: $user_agent = $_SERVER['HTTP_USER_AGENT'];if (strpos($user_agent, 'MicroMessenger') === false) ...
- android bug archive
console提示: No Launcher activity found! The launch will only sync the application package on the devi ...
- RGBA与半透明背景
概念 所谓RGBA颜色,就是RGB三原色加ALPHA.在给背景加入颜色的同一时候.提供透明度特性. 用法 background:rgba(90,90, 54, 0.5); 支持情况 Firefox 3 ...
- uitableview滚动到最后一行
本文转载至 http://mrjeye.iteye.com/blog/1278521 - (void)scrollTableToFoot:(BOOL)animated { NSInteger s = ...
- [luogu4755]Beautiful Pair
[luogu4755]Beautiful Pair luogu 第一次写最大值分治感觉有点丑 每次找到最大值mid,扫小的一边,主席树查大的一边小于等于\(\frac{a[mid]}{a[i]}\)的 ...
- PHPUnit学习记录
今天是2017-1-17号,昨晚收到邮件,被view code之后,基本全部需要重构,其实我写得php代码里面完全是东拼西凑的代码,自己都不知道什么意思,今天被要求学习PHPUnit了 ------- ...
- ansible copy文件比较慢, 使用Synchronize模块
Ansible中的同步模块(rsync) Synchronize模块 1 2 3 4 5 6 7 This is a wrapper around rsync. Of course you cou ...
- R语言数据管理(四):数据导出
与read.*函数对应,导出函数为write.*函数. 比较常见的为write.csv和write.table. 一般格式: setwd("D:\\") write.table(y ...