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 ...
随机推荐
- Pyqt4 360界面风格皮肤实现
前言 最近用Pyqt做了软件界面,始终觉得windows风格不太好看,虽然数字公司的行为有争议,但是也不影响我欣赏360卫士的界面风格. 声明 首先声明,此项工作并非原创,而是基于这位zhuyeqin ...
- mysql忘记password
有时候突然忘记MySQL的password会真的不爽,这里介绍一种MySQLpassword忘记时重置password的方法,操作系统win8,MySql version:5.6.10 1 在任务管理 ...
- Dubbo--简单介绍
Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,能够和Spring框架无缝集成.Dubbo致力于提供高性能和透明化的RPC远程服务调用 ...
- JS——特效秀
0.凛冬将至,用几款特效暖暖身 ①.tab图片切换: ②.索引图片切换: ③.统计图: ④.滚动条分页: 1.Canvas跳动彩球时间动画特效
- PostgreSQL 封装操作数据库方法
/// <summary> /// 模块名:操作postgres数据库公共类 /// 作用:根据业务需求对数据库进行操作. /// 注:系统中的公共方法,根据需要,逐一引入 /// 作者: ...
- java ResultSet获得总行数
在Java中,获得ResultSet的总行数的方法有以下几种. 第一种:利用ResultSet的getRow方法来获得ResultSet的总行数 Statement stmt = con.create ...
- Android中的android:layout_width和android:width
最近在看android的东西,发现很多和web前台的东西一样(思想).只是看到很多属性的写法和前台有差别,刚刚看到这样的属性: android:width 其实是定义控件上面的文本(TextView) ...
- Android OOM解决方案 :
清单文件里 给Application标签加上android:largeHeap="true"这行代码 这样会给你的app分配一个大内存 如果某个页面在绘制时会耗非常多的内存 ...
- Little-endian和Big-endian模式
这段C程序的结果是多少? 嵌入式系统开发者应该对Little-endian和Big-endian模式非常了解.采用Little-endian模式的CPU对操作数的存放方式是从低字节到高字节,而Big- ...
- Win10上Python3通过pip安装时出现UnicodeDecodeError
http://blog.csdn.net/qq_33530388/article/details/68933201 解决方法: 打开 c:\program files\python36\lib\sit ...