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 ...
随机推荐
- nodejs初学-----helloworld
近期紧锣密鼓的学习了下nodejs(之前在学php.算入门了吧,可是时间关系,还没写文章,兴许要搞安卓和大数据,总之比較忙哈,计划上php要排到后面了,还请广大小伙伴不要着急) 先抄一句:Node.j ...
- 数据库sql的join多表
摘录文章 SQL join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据.注意,join后的数据记录数不一定就是左或右表的简单连接,图表只代表集合关系,在数量上并不准确,如这个条件后结果, ...
- python学习(七)字典学习
#!/usr/bin/python # 字典 # 当时学java的时候, 语言基础就学了好久, 然后是各种API, 最后才是集合 # 键值对, 可变 # 1. 映射操作 D = {'food' : ' ...
- 深入Asyncio(十一)优雅地开始与结束
Startup and Shutdown Graceful 大部分基于asyncio的程序都是需要长期运行.基于网络的应用,处理这种应用的正确开启与关闭存在惊人的复杂性. 开启相对来说更简单点,常规做 ...
- hdu 5538 House Building(长春现场赛——水题)
题目链接:acm.hdu.edu.cn/showproblem.php?pid=5538 House Building Time Limit: 2000/1000 MS (Java/Others) ...
- Yii2 跨库orm实现
近期在对公司的Yii2项目进行子系统拆分,过度阶段难免会有一些跨库操作,原生语句还好,加下库名前缀就可以了,可是到了orm问题就来了,特别是用到model做查询的时候,现在来记录一下跳过的坑, 像下面 ...
- python 基础 4.2 高阶函数上
一.高阶函数 把函数当做参数传递的一种函数 1>map()函数 map函数是python内置的一个高阶函数,它接受一个函数f和一个list,并把list元素以此传递给函数f,然后返回一个函数 ...
- C# 自定义控件制作和使用实例(winform)(转)
本例是制作一个简单的自定义控件,然后用一个简单的测试程序,对于初学者来说,本例子比较简单,只能起到抛石引玉的效果. 我也是在学习当中,今后会将自己所学的逐步写出来和大家交流共享. 第一步:新建一个 ...
- PCA tries to preserve linear structure, MDS tries to preserve global geometry, and t-SNE tries to preserve topology (neighborhood structure)
https://colah.github.io/posts/2014-10-Visualizing-MNIST/
- cocos2d-js添加艾盟插屏(通过jsb反射机制)
1.导入jar包 2.修改AndroidManifest.xml文件 添加: <activity android:name="com.xingka ...