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. MySQL 优化、设计规则浅谈

    当数据量大,数据库相应慢时都会针对数据库进行优化.这时都是要针对具体情况,具体业务需求进行优化的. 但是有些步骤和规则应该适合各种情况的.这里综合网上找的资料简单分析一下. 第一优化你的sql和索引: ...

  2. jdbc 链接池

    package cn.itcast.jdbc.datasourse; import java.sql.Connection;import java.sql.DriverManager;import j ...

  3. 四、Silverlight中使用MVVM(四)——演练

    本来打算用MVVM实现CRUD操作的,这方面例子网上资源还挺多的,毕竟CRUD算是基本功了,因为最近已经开始学习Cailburn框架了,感觉时间 挺紧的,这篇就实现其中的更新操作吧. 功能很明确,当我 ...

  4. LeetCode(100)题解--Same Tree

    https://leetcode.com/problems/same-tree/ 题目: Given two binary trees, write a function to check if th ...

  5. CentOS系统环境下安装MongoDB

    (1)进入MongoDB下载中心:http://www.mongodb.org/downloads We recommend using these binary distributions (官方推 ...

  6. 错误记录--更改tomcat端口号方法,Several ports (8005, 8080, 8009)【转】

    启动Tomcat服务器报错: Several ports (8005, 8080, 8009) required by Tomcat v5.5 Server at localhost are alre ...

  7. Socket的错误码和描述

    //下面是Socket Error的错误码和描述: Socket error 0 - Directly send error  Socket error 10004 - Interrupted fun ...

  8. ubuntu 安装 pygame 很好玩的东西

    1. 简介 pygame 是基于对 SDL库的python 封装,提供python接口.SDL(Simple DirectMedia Layer) 是一个跨平台的游戏开发库,方便游戏开发和移植.目前最 ...

  9. 我的Java开发学习之旅------>工具类:Java获取字符串和文件进行MD5值

    ps:这几天本人用百度云盘秒传了几部大片到云盘上,几个G的文件瞬秒竟然显示"上传成功"!这真让我目瞪口呆,要是这样的话,那得多快的网速,这绝对是不可能的,也许这仅是个假象.百度了一 ...

  10. 【足迹C++primer】35、特定容器算法

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/cutter_point/article/details/33732681 特定容器算法 lst.me ...