Codeforces 762D

题目大意:

给定一个\(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 动态规划的更多相关文章

  1. CodeForces 762D Maximum path

    http://codeforces.com/problemset/problem/762/D 因为是3*n很巧妙的地方是 往左走两步或更多的走法都可以用往回走以一步 并走完一列来替换 那么走的方法就大 ...

  2. cf 762D. Maximum path

    天呢,好神奇的一个DP23333%%%%% 因为1.向左走1格的话相当于当前列和向左走列全选 2.想做走超过1的话可以有上下走替代.而且只能在相邻行向左. 全选的情况只能从第1行和第3行转移,相反全选 ...

  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 ...

  4. 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. ...

  5. 二叉树系列 - 二叉树里的最长路径 例 [LeetCode] Binary Tree Maximum Path Sum

    题目: Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start ...

  6. [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. ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. slam cartographer 学习

    https://github.com/slam4code                   感谢大牛的分享

  2. Net dll版本兼容问题

    Net dll组件版本兼容问题 https://www.cnblogs.com/newP/p/9543528.html dll组件版本兼容问题,是生产开发中经常遇到的问题,常见组件兼容问题如:Newt ...

  3. C#游戏开发高速新手教程Unity5.5教程

    C#游戏开发高速新手教程Unity5.5教程 试读文档下载地址:http://pan.baidu.com/s/1slwBHoD C#是微软公布的高级程序设计语言.这门语言和C语言一样,已经成为了大学计 ...

  4. HDU 5379 Mahjong tree(树的遍历&amp;组合数学)

    本文纯属原创,转载请注明出处.谢谢. http://blog.csdn.net/zip_fan 题目传送门:http://acm.hdu.edu.cn/showproblem.php? pid=537 ...

  5. DB2 时间操作

    1. SQL语句得到当前的日期,时间和时间戳    SELECT current date FROM sysibm.sysdummy1;      SELECT current time FROM s ...

  6. 【BZOJ1095】[ZJOI2007]Hide 捉迷藏 动态树分治+堆

    [BZOJ1095][ZJOI2007]Hide 捉迷藏 Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子们决定在家里玩捉 ...

  7. 注册HttpHandler

    How to: Register HTTP Handlers After you have created a custom HTTP handler class, you must register ...

  8. what is spring and what is spring for

    1 what is spring spring是一个轻量级的容器. 它使用依赖注入技术来构建耦合性很低的系统. 2 what is  spring for 用于系统的依赖解耦合.在一个系统中,A类依赖 ...

  9. 如何修改硬盘挂载的名字LABEL

    ➜ ~ df -h Filesystem Size Used Avail Use% Mounted on/dev/sda2 114G 97G 12G 90% /media/brian/4ef34b75 ...

  10. 在pycharm中执行脚本没有报错但输出显示Redirection is not supported.

    没有新式语法错误,但是输出显示Redirection is not supported.(不支持重定向) 在stockflow中找到是因为从IDE中运行脚本的原因,比如pycharm,所有IDE都提供 ...