Codeforces 762D Maximum path 动态规划
题目大意:
给定一个\(3*n(n \leq 10^5)\)的矩形,从左上角出发到右下角,规定每个格子只能经过一遍。经过一个格子会获得格子中的权值。每个格子的权值\(a_{ij}\)满足\(-10^9 \leq a_{ij} \leq 10^9\).最大化收益
题解:
乍一看,好麻烦!
最主要的是因为他能够往回走.
但是我们画图可以发现:每次往回走一定不用超过1次.
也就是说,最多只能走成这样

而不会走成这样

因为下图的走法一定可以用上图组合,并且
由于只用3行的特性,每次向回走实际上是取走了所有的数.
所以我们只采用上图方式得出来的答案一定最优
所以我们O(n)线性递推即可
设\(f[i][j]\)为到达第i列第j行的最大收益
方程比较多,就不写了,自己看代码吧。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
template<typename T>inline T cat_max(const T &a,const T &b){return a>b ? a:b;}
template<typename T>inline T cat_min(const T &a,const T &b){return a<b ? a:b;}
const int maxn = 100010;
ll w[maxn][6],f[maxn][6],g[maxn][6];
int main(){
int n;read(n);
for(int i=1;i<=n;++i) read(w[i][1]);
for(int i=1;i<=n;++i) read(w[i][2]);
for(int i=1;i<=n;++i) read(w[i][3]);
f[1][1] = w[1][1];
f[1][2] = w[1][1] + w[1][2];
f[1][3] = w[1][1] + w[1][2] + w[1][3];
g[1][1] = w[1][1];g[1][2] = w[1][2];g[1][3] = w[1][3];
for(int i=2;i<=n;++i){
f[i][1] = g[i][1] = f[i-1][1] + w[i][1];
f[i][2] = g[i][2] = f[i-1][2] + w[i][2];
f[i][3] = g[i][3] = f[i-1][3] + w[i][3];
f[i][1] = cat_max(f[i][1],g[i][2] + w[i][1]);
f[i][1] = cat_max(f[i][1],g[i][3] + w[i][2] + w[i][1]);
f[i][2] = cat_max(f[i][2],g[i][1] + w[i][2]);
f[i][2] = cat_max(f[i][2],g[i][3] + w[i][2]);
f[i][3] = cat_max(f[i][3],g[i][2] + w[i][3]);
f[i][3] = cat_max(f[i][3],g[i][1] + w[i][2] + w[i][3]);
f[i][1] = cat_max(f[i][1],g[i-1][3] + w[i][3] + w[i][2] + w[i-1][2] + w[i-1][1] + w[i][1]);
f[i][3] = cat_max(f[i][3],g[i-1][1] + w[i][1] + w[i][2] + w[i-1][2] + w[i-1][3] + w[i][3]);
}
printf("%I64d",f[n][3]);
getchar();getchar();
return 0;
}
Codeforces 762D Maximum path 动态规划的更多相关文章
- CodeForces 762D Maximum path
http://codeforces.com/problemset/problem/762/D 因为是3*n很巧妙的地方是 往左走两步或更多的走法都可以用往回走以一步 并走完一列来替换 那么走的方法就大 ...
- cf 762D. Maximum path
天呢,好神奇的一个DP23333%%%%% 因为1.向左走1格的话相当于当前列和向左走列全选 2.想做走超过1的话可以有上下走替代.而且只能在相邻行向左. 全选的情况只能从第1行和第3行转移,相反全选 ...
- [leetcode]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum
题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- LeetCode(124) Binary Tree Maximum Path Sum
题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...
- LeetCode124:Binary Tree Maximum Path Sum
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- leetcode 124. Binary Tree Maximum Path Sum
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
随机推荐
- 调试Scrapy过程中的心得体会
1.大量抓取网页时出现“Memory Error”解决办法:设置一个队列,每当爬虫空闲时才向队列中放入请求,例如: from scrapy import signals, Spider from sc ...
- Cocoapods完整使用篇
温馨提示:在篇文章中所使用的Xcode版本为Xcode7. 一.什么是CocoaPods? 简单来说,就是专门为iOS工程提供对第三方库的依赖的管理工具,通过CocoaPods,我们可以单独管理每 ...
- kotlin 语言入门指南(一)--基础语法
基于官网的Getting Start的基础语法教程部分,一共三节,这篇是第一节,翻译如下: 基础语法 定义一个包 包的声明必须放在文件头部: package my.demo import java.u ...
- rtems 4.11 部分m4文件分析
本来想把configure.ac和各种m4文件分析明白,发现有点困难,不过好在也能理解一些. 基本教程 首先要明白m4,参见这个教程,写得不错,不论怎么样m4替换来替换去的,还真是不那么容易懂,好在我 ...
- 15 nginx反向代理实现nginx+apache动静分离
一:nginx反向代理实现nginx+apache动静分离-------------概念--------------------------- nginx反向代理服务器+负载均衡 用nginx做反向代 ...
- squared-error loss is much more repaidly updated than mean-absolute-deviation when searching for splits
平方差损失能较绝对值差损失更快地更新
- The Log-Structured Merge-Tree (LSM-Tree
https://www.cs.umb.edu/~poneil/lsmtree.pdf [Log-Structured Merge-Tree ][结构化日志归并树][要解决的问题]The Log-S ...
- Python爬虫-- BeautifulSoup库
BeautifulSoup库 beautifulsoup就是一个非常强大的工具,爬虫利器.一个灵活又方便的网页解析库,处理高效,支持多种解析器.利用它就不用编写正则表达式也能方便的实现网页信息的抓取 ...
- 性能测试--siege
siege 这是Linux系统下的一个测试工具,完全使用C语言实现,可以对HTTP和FTP服务器进行负载和性能测试.通过使用Siege 提供的功能,可以很容易的制定测试计划:包括规定使用并发用户数.重 ...
- Swift语言概览
Swift语言概览 关于 这篇文章简要介绍了苹果于WWDC 2014公布的编程语言--Swift. 前言 在这里我觉得有必要提一下Brec Victor的Invent ...