CF762D Maximum Path
题目戳这里。
首先明确一点,数字最多往左走一次,走两次肯定是不可能的(因为只有\(3\)行)。
然后我们用\(f_{i,j}\)表示前\(i\)行,第\(i\)行状态为\(j\)的最优解。(\(j\)表示从第一,二,三,行出来,或者是朝左走了)。
方程应该也好YY。
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef long long ll;
const int maxn = 100010; const ll inf = 1LL<<60;
int N; ll f[maxn][4],A[4][maxn];
inline int gi()
{
char ch; int ret = 0,f = 1;
do ch = getchar(); while (!(ch >= '0'&&ch <= '9')&&ch != '-');
if (ch == '-') f = -1,ch = getchar();
do ret = ret*10+ch-'0',ch = getchar(); while (ch >= '0'&&ch <= '9');
return ret*f;
}
int main()
{
freopen("762D.in","r",stdin);
freopen("762D.out","w",stdout);
N = gi();
for (int i = 1;i <= 3;++i) for (int j = 1;j <= N;++j) A[i][j] = gi()+A[i-1][j];
for (int i = 0;i <= N;++i) for (int j = 0;j < 4;++j) f[i][j] = -inf;
f[0][1] = 0;
for (int i = 1;i <= N;++i)
{
for (int j = 1;j <= 3;++j)
for (int k = 1;k <= 3;++k) f[i][j] = max(f[i-1][k]+A[max(j,k)][i]-A[min(j,k)-1][i],f[i][j]);
f[i][1] = max(f[i][1],f[i-1][0]+A[3][i]);
f[i][3] = max(f[i][3],f[i-1][0]+A[3][i]);
f[i][0] = max(f[i][0],max(f[i-1][1],f[i-1][3])+A[3][i]);
}
cout << f[N][3] << endl;
fclose(stdin); fclose(stdout);
return 0;
}
CF762D Maximum Path的更多相关文章
- 题解 CF762D Maximum path
题目传送门 Description 给出一个 \(3\times n\) 的带权矩阵,选出一个 \((1,1)\to (3,n)\) 的路径使得路径上点权之和最大. \(n\le 10^5\) Sol ...
- [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. ...
随机推荐
- .Net Core On Liunx 环境搭建之安装Mysql8
上一篇文章安装了MongoDB紧接上一篇随笔,来进行MySql数据库的安装 服务器环境:阿里云云服务器,操作系统CentOS.7-x64 注:文章的图片是我从我的CSDN博客中直接粘贴过来的,不是扒的 ...
- 搞笑入群二维码在线生成源码 php图片合成并添加文字水印
在凤凰网看到一篇文章:微信群二维码也能“整人”,99%的好友会中招!感觉挺好玩,所以自己也想做一个! 冷静分析
- mysql在cmd里中文乱码解决办法
右边画红线部分中文已经乱码,左边红线里中文则完美显示出来了. 解决办法 用set names utf-8: 效果如图
- C语言学习记录_2019.02.02
变量在第一次被使用之前应该赋初值 scanf(“%d”,&price); scanf(“price%d %d”,&price); scanf中的东西一定是要输入的东西. 定义常量:c ...
- iScroll实现下拉刷新上拉加载
前言 初学iscroll这个控件,给我的一个感觉还是蛮不错的. 什么是iScroll:是目前最成熟的自定义滚动解决方案之一,在移动端和PC有很好的兼容性.iScroll官方提供了5个不同的版本 isc ...
- ULINE(插入水平线)
WRITE 'This is Underlined'. ULINE. 输出结果: This is Underlined. ———————————————————
- Koa基本使用
简介 koa 是由 Express 原班人马打造的,致力于成为一个更小.更富有表现力.更健壮的 Web 框架. 使用 koa 编写 web 应用,通过组合不同的 generator,可以免除重复繁琐的 ...
- jmeter之Synchronizing Timer的理解
该功能类似loadrunner的集合点,一般按照jmeter的树形结构,放在需要设置集合点的请求之前,两个参数的意思,我们先看官网的解释: 大概意思就是: Number of Simulated Us ...
- xshell、xftp免费版下载方法
第一步:进入官站 https://www.netsarang.com/ 第二步:选中Free License
- LCA(最近公共祖先)——离线 Tarjan 算法
tarjan算法的步骤是(当dfs到节点u时):1 在并查集中建立仅有u的集合,设置该集合的祖先为u1 对u的每个孩子v: 1.1 tarjan之 1.2 合并v到父节点u的集合,确保集合的祖 ...