题解 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 ...
随机推荐
- python操作图片
时间:2018-11-30 记录:byzqy 标题:python实现图片操作 地址:https://blog.csdn.net/baidu_34045013/article/details/79187 ...
- T-SQL - query02_查看数据库信息|查看服务器名称|查看实例名
时间:2017-09-29 编辑:byzqy 本篇记录几个查询数据库信息的 T-SQL 语句: 查看数据库信息 查看服务器名称 查看实例名 文件:SQLQuery2.sql /* 说明: SQLQue ...
- Session原理、生命周期及购物车功能的实现
在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据(保存该浏览器(会话)的相关信息)时 ...
- introduction-to-64-bit-assembly
introduction-to-64-bit-assembly NASM - The Netwide Assembler x86-64 下函数调用及栈帧原理 汇编语言基本概念简介 mycode
- Qt中的Q_PROPERTY宏浅析
1. Q_PROPERTY Qt提供了一个绝妙的属性系统,Q_PROPERTY()是一个宏,用来在一个类中声明一个属性property,由于该宏是qt特有的,需要用moc进行编译,故必须继承于QObj ...
- 数据导出生成Excel附件使用POI的HSSFWorkbook对象
比较常用的实现Java导入.导出Excel的技术有两种Jakarta POI和Java Excel.Jakarta POI 是一套用于访问微软格式文档的Java API.Jakarta POI有很多组 ...
- 远程线程注入DLL突破session 0 隔离
远程线程注入DLL突破session 0 隔离 0x00 前言 补充上篇的远程线程注入,突破系统SESSION 0 隔离,向系统服务进程中注入DLL. 0x01 介绍 通过CreateRemoteTh ...
- IDEA SpotBugs代码安全审计插件
IDEA SpotBugs代码安全审计插件 在寻找idea代码审计插件的时候,发现Findbugs已经停止更新,无法在idea2020.01版本运行,由此找到SpotBugs SpotBugs介绍 S ...
- 【第六篇】- Maven 仓库之Spring Cloud直播商城 b2b2c电子商务技术总结
Maven 仓库 在 Maven 的术语中,仓库是一个位置(place). Maven 仓库是项目中依赖的第三方库,这个库所在的位置叫做仓库. 在 Maven 中,任何一个依赖.插件或者项目构建的输出 ...
- prometheus从零开始
本次的想法是做服务监控 并告警 主要线路如下图所示 1.运行prometheus docker方式 docker run -itd \ -p 9090:9090 \ -v /opt/prometh ...