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. ...
随机推荐
- [心得]暑假DAY 5
好久没更新博客了 最近事情太多太多 tarjan进阶,点双边双 T2压力 最大坑点:点双缩点 它不是直接把割点连成树(割点会有环) 而是用割点作”中介“,联接点双构成一颗树(所谓圆方树) 接着在上面进 ...
- laravel中跟据某个特定顺序去排序查出来的数据:FIND_IN_SET
//返回有顺序的客户id $customer_ids = $customer->bespeakTime($uid); $res = Customer::with('customer_indust ...
- Socket——实现一个简单的静态网页服务器
整体结构就是使用ServerSocket监听一个地址,当有接受到请求之后,解析请求中的资源路径.服务器资源路径存放在项目下的一个目录中,服务器会到这个目录中根据请求的路径去寻找相应的资源.如果找到了则 ...
- grep命令和tail命令
写在前面的话: 最近参与了新项目开发,周期短,与自己负责的主要业务对接.业务复杂,时常出现bug,然额对于菜鸟的我,更是无从下手.其实最好的帮助就是 学会查看日志,关键是之前查看日志真是太少了,菜鸟一 ...
- Zabbix - 配置服务器对第三方服务的监控
需求: 需要配置zabbix监控,使得zabbix服务器可以监控到另一台服务器上运行的第三方服务的状态,当状态异常时发送告警邮件. 限制:被监控的服务器不允许安装任意客户端,且该台服务器不能联通外网 ...
- MVC模式入门案例
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widg ...
- tensorflow二进制文件读取与tfrecords文件读取
1.知识点 """ TFRecords介绍: TFRecords是Tensorflow设计的一种内置文件格式,是一种二进制文件,它能更好的利用内存, 更方便复制和移动,为 ...
- Appium移动自动化测试(四)之元素定位
做过UI自动化测试的童鞋都会发现, 在上一篇文章中居然没有万能定位方式Xpath. 是滴, 确实没有! ADT自带的uiautomatorviewer里面并没有属性xpath, 如果我们需要的话,还需 ...
- FAQ_2
FAQ-2 1.LoadRunner超时错误: 在录制Web服务器端,如果超过120秒服务器协议脚本回放时超时情况经常出现,产生错误的原因也有很多,解决的方法也不同. 错误现象1:Action.c(1 ...
- js 闭包解决方案
相比很多人都遇见过这样的情况: 给一个ul的所有li绑定行为,在点击时获取其index. 假设结构如下: <body> <ul> <li>0</li> ...