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. ...
随机推荐
- Linux Shell常用命令(长期更新)
#判断某个字段是否匹配指定值 awk -F"," '{if($4=="value"){print $1} else {print $0}}' file.txt ...
- laydate js动态添加时间
$("#test2").click(function(){ var input=$('<input/>'); $("#test1").append( ...
- html 弹框 优化 alert
<!DOCTYPE html> <html> <head> <title>cs</title> </head> <styl ...
- linux中常用命令总结
一关机/重启/注销 关机 shutdown -h now //立即关机 重启 shutdown -r now //立即重启 reboot 重新启动 注销 logout //退出注销当前用户窗口 exi ...
- POLYGON(动态规划)
学校老师布置的一道动规的题目,要求下次上课前AC.周一一放学就回家写,调试了一会儿OK了.在这边记录一下解题的思路和过程,也作为第一篇随笔,就是随便之一写,您也就随便之一看.有问题望你指出,多多包涵. ...
- 2018 ccpc final I. Cockroaches
I. Cockroaches time limit per test6. s memory limit per test256 MB inputstandard input outputstandar ...
- 【Leetcode】Jewels and Stones
Jewels and Stones Description You're given strings J representing the types of stones that are jewel ...
- debounce、throttle、requestAnimationFrame
今天review同事代码,代码实现了返回顶部的功能,用到了lodash库中的throttle,我看着眼生,于是乎去看了下lodash文档,然后牵出了debounce,具体的知识点,这里不再赘述,底部的 ...
- Spring MVC怎么统一异常管理?
1. 在类上加上@ControllerAdvice注解 2. 在方法上加上@ExceptionHandler注解 @ExceptionHandler(Exception.class) @Respons ...
- Grok Debugger本地安装(转载)
原文链接:http://fengwan.blog.51cto.com/508652/1758845 最近在使用ELK对日志进行集中管理,因为涉及到日志的规则经常要用到http://grokdebug. ...