Maximum path CodeForces - 762D
大意: 3*n矩阵, 求从(1,1)->(3,n)路径最大点权和.
核心观察是每个点回头一定不会超过1, 这是因为只有三行, 若回头两格一定是$9$个位置全走, 显然可以找到一种只会头一格的方案与回头两格的方案等价.
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll; const int N = 1e6+10;
int n;
ll a[3][N], dp[3][N], s[N]; int main() {
scanf("%d", &n);
REP(i,0,2) REP(j,1,n) scanf("%lld",a[i]+j);
REP(i,1,n) s[i]=a[0][i]+a[1][i]+a[2][i];
dp[0][1]=a[0][1],dp[1][1]=a[0][1]+a[1][1],dp[2][1]=s[1];
REP(i,2,n) {
dp[0][i]=max({dp[0][i-1],dp[1][i-1]+a[1][i],dp[2][i-1]+a[2][i]+a[1][i]});
dp[0][i]=max(dp[0][i],dp[2][i-2]+s[i-1]+a[2][i]+a[1][i]);
dp[0][i]+=a[0][i];
dp[2][i]=max({dp[2][i-1],dp[1][i-1]+a[1][i],dp[0][i-1]+a[0][i]+a[1][i]});
dp[2][i]=max(dp[2][i],dp[0][i-2]+s[i-1]+a[0][i]+a[1][i]);
dp[2][i]+=a[2][i];
dp[1][i]=max({dp[1][i-1],dp[0][i-1]+a[0][i],dp[2][i-1]+a[2][i]});
dp[1][i]+=a[1][i];
}
printf("%lld\n",dp[2][n]);
}
Maximum path CodeForces - 762D的更多相关文章
- Codeforces 762D Maximum path 动态规划
Codeforces 762D 题目大意: 给定一个\(3*n(n \leq 10^5)\)的矩形,从左上角出发到右下角,规定每个格子只能经过一遍.经过一个格子会获得格子中的权值.每个格子的权值\(a ...
- [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]Binary Tree Maximum Path Sum
Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...
- 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 ...
- [lintcode] Binary Tree Maximum Path Sum II
Given a binary tree, find the maximum path sum from root. The path may end at any node in the tree a ...
- 【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 ...
- 【leetcode】Binary Tree Maximum Path Sum (medium)
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
随机推荐
- C++ this指针的理解
先要理解class的意思.class应该理解为一种类型,象 int,char一样,是用户自定义的类型.虽然比int char这样build-in类型复杂的多,但首先要理解它们一样是类型.用这个类型可以 ...
- Java - 单链表
链表是一种常见的基础数据结构,是一种有序的列表,但不会按照线性顺序存储数据,而是在每一个节点里存储下一个节点的指针(next).链表适合插入.删除,不宜过长,否则会导致遍历性能下降. 以节点方式存储: ...
- LeetCode 73. 矩阵置零(Set Matrix Zeroes)
题目描述 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1 ...
- koa 基础(十七)原生 JS 中的类、静态方法、继承
1.app.js /** * 原生 JS 中的类.静态方法.继承 * es5中的类和静态方法 */ function Person(name, age) { // 构造函数里面的方法和属性 this. ...
- 编译内核时报错./include/net/sch_generic.h:535:28: error: inlining failed in call to always_inline 'qdisc_pkt_len': indirect function call with a yet undetermined callee static inline unsigned int qdisc_pkt_
直接修改头文件include/net/sch_generic.h中的qdisc_pkt_len函数 将static inline unsigned int qdisc_pkt_len修改为: stat ...
- DDNS是动态域名解析的意思
目前大部分家庭使用PPPOE拨号方式上网,每次上网获得的IP都是随机变换的,但是家里的网络监控.智能设备需要通过网络访问,每次使用都需要先知道IP非常麻烦. 有了DDNS动态域名解析,我们只要到花生壳 ...
- 关于ansbile
YAML语法规则 规则一:缩进(一个缩进两空格,注意一定不用tab) 规则二:冒号(每个冒号后一定要有空格) 规则三:短横线 - (短横线后面要空格) 编写案例 ansible-playbook -- ...
- image-webpack-loader在mac或ubuntu报错
解决办法安装libpng库,在github issue https://github.com/tcoopman/image-webpack-loader/issues/49可查看 mac: brew ...
- Qt 之水平/垂直布局(QBoxLayout、QHBoxLayout、QVBoxLayout)
简述QBoxLayout可以在水平方向或垂直方向上排列控件,由QHBoxLayout.QVBoxLayout所继承. QHBoxLayout:水平布局,在水平方向上排列控件,即:左右排列. QVBox ...
- C# 函数返回多个值的方法
有时候我们需要一个函数返回多个值,网上更多是用out实现,我个人很喜欢用tuple方法. tuple是一个元组,最多支持7个元素,再多需要嵌套等方法实现. 使用元组定义函数的方法如下: public ...