题解 CF762D Maximum path
Description
给出一个 \(3\times n\) 的带权矩阵,选出一个 \((1,1)\to (3,n)\) 的路径使得路径上点权之和最大。
\(n\le 10^5\)
Solution
感觉挺妙的一个题,不知道为什么在 CF 上评分只有 2300,或许是因为外国人科技树比较偏。/kk
可以想到的是,任何左走的情况一定都可以变为每次只往左边走一格的情况,那么我们就可以直接 dp 了。
Code
#include <bits/stdc++.h>
using namespace std;
#define Int register int
#define int long long
#define MAXN 100005
template <typename T> inline void read (T &t){t = 0;char c = getchar();int f = 1;while (c < '0' || c > '9'){if (c == '-') f = -f;c = getchar();}while (c >= '0' && c <= '9'){t = (t << 3) + (t << 1) + c - '0';c = getchar();} t *= f;}
template <typename T,typename ... Args> inline void read (T &t,Args&... args){read (t);read (args...);}
template <typename T> inline void write (T x){if (x < 0){x = -x;putchar ('-');}if (x > 9) write (x / 10);putchar (x % 10 + '0');}
template <typename T> inline void chkmax (T &a,T b){a = max (a,b);}
template <typename T> inline void chkmin (T &a,T b){a = min (a,b);}
int n,a[3][MAXN],f[5][MAXN];
int getSum (int t,int l,int r){
if (l > r) swap (l,r);int sum = 0;
for (Int i = l;i <= r;++ i) sum += a[i][t];
return sum;
}
signed main(){
read (n);
for (Int i = 0;i < 3;++ i)
for (Int k = 1;k <= n;++ k) read (a[i][k]);
memset (f,0xcf,sizeof (f)),f[0][0] = 0;
for (Int i = 1;i <= n;++ i){
for (Int j = 0;j < 3;++ j)
for (Int k = 0;k < 3;++ k)
chkmax (f[j][i],f[k][i - 1] + getSum (i,j,k));
chkmax (f[0][i],max (f[3][i - 1] + a[0][i],f[4][i - 1] + getSum (i,0,2)));
chkmax (f[2][i],max (f[3][i - 1] + getSum (i,0,2),f[4][i - 1] + a[2][i]));
chkmax (f[1][i],max (f[3][i - 1] + getSum (i,0,1),f[4][i - 1] + getSum (i,1,2)));
chkmax (f[3][i],f[2][i - 2] + getSum (i - 1,0,2) + getSum (i,0,2));
chkmax (f[4][i],f[0][i - 2] + getSum (i - 1,0,2) + getSum (i,0,2));
}
write (max (f[2][n],f[4][n])),putchar ('\n');
return 0;
}
题解 CF762D Maximum path的更多相关文章
- CF762D Maximum Path
题目戳这里. 首先明确一点,数字最多往左走一次,走两次肯定是不可能的(因为只有\(3\)行). 然后我们用\(f_{i,j}\)表示前\(i\)行,第\(i\)行状态为\(j\)的最优解.(\(j\) ...
- Binary Tree Maximum Path Sum leetcode java
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tr ...
- 【Lintcode】094.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] 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 ...
随机推荐
- docker学习之network:初识网络配置
起因 我的开发环境需要一个python代码运行环境.reids服务和mysql服务. 由于以前,我的开发环境是mac,而CI和线上运行环境是centos,偶尔会出项本地单元测试跑不过,而CI可以过.这 ...
- OVN入门
参考链接 如何借助 OVN 来提高 OVS 在云计算环境中的性能 OVN简介 Open vSwitch Documentation OVSDB介绍及在OpenDaylight中的调用 OpenDayl ...
- JAVA《多线程多人上线通知案例》
package com.wangbiao.palyermanager; import com.wangbiao.player.Player; /** * TODO * * @author wangbi ...
- Linux - last 命令(Mac 电脑)
前言 为啥写这篇? 因为听 grep.sed 教程的时候有这个命令栗子 加上工作中,运维给我排查问题的时候也用到了,感觉挺重要,先了解为敬! 命令作用 显示用户和TTY的最后登录次数 这个是在 Mac ...
- redis跨实例迁移 & redis上云
1)redis跨实例迁移--源实例db11迁移至目标实例db30 root@fe2e836e4470:/data# redis-cli -a pwd1 -n 11 keys \* |while rea ...
- Tensorflow保存神经网络参数有妙招:Saver和Restore
摘要:这篇文章将讲解TensorFlow如何保存变量和神经网络参数,通过Saver保存神经网络,再通过Restore调用训练好的神经网络. 本文分享自华为云社区<[Python人工智能] 十一. ...
- Redis-数据类型-应用场景
目录 一些小问题 String Hash List Set ZSet BitMaps Hyperloglog Geo Streams 应用场景小结 一些小问题 Redis一共有几种数据类型?(注意是数 ...
- Spirit带你彻底了解事件捕获和冒泡机制
Dom标准事件模型 在Dom标准事件模型中,事件是先进行捕获,达到目标阶段时,在进行冒泡的 捕获阶段==>目标阶段==>冒泡阶段 目标元素和非目标元素 在介绍事件捕获和事件冒泡前 我们先要 ...
- python中字符串的各种方法
图片来源见水印,一个学python的公众号
- windows 下使用 mingw编译器 调试时 无法跟进源码
windows 下使用 mingw编译器 调试时 无法跟进源码 最近在公司使用QT 开发,官方在线下载的 安装的QT mingw 都是没有debug版本的 由于没有debug版本动态库 所以你调试的时 ...