题解 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 ...
随机推荐
- DFS常规解题套路
本文为xdfApp团队成员文章,原文链接:https://blog.csdn.net/sinat_37380158/article/details/106866970 作者介绍:韩沛沛, 北京邮电大学 ...
- IDEA中的Git操作,看这一篇就够了!
大家在使用Git时,都会选择一种Git客户端,在IDEA中内置了这种客户端,可以让你不需要使用Git命令就可以方便地进行操作,本文将讲述IDEA中的一些常用Git操作. 环境准备 使用前需要安装一个远 ...
- vue-cli3.x中的webpack配置,优化及多页面应用开发
官方文档 vue-cli3以下版本中,关于webpack的一些配置都在config目录文件中,可是vue-cli3以上版本中,没有了config目录,那该怎么配置webpack呢? 3.x初始化项目后 ...
- ubuntu18.04 更换镜像源
废话不多说,直接上图了 1. 首先选software & update 2. 点这个,然后选择others,选择China 建议选择清华源,不建议选择mirrors.aliyun.com,因为 ...
- Springboot 整合通用mapper和pagehelper展示分页数据(附github源码)
简介 springboot 设计目的就是为了加速开发,减少xml的配置.如果你不想写配置文件只需要在配置文件添加相对应的配置就能快速的启动的程序. 通用mapp 通用mapper只支持对单表的操作,对 ...
- 文件流转换(一般用于axios设置接收文件流设置时responseType: 'blob')
文件流转换 一般用于axios设置接收文件流设置时responseType: 'blob'当接口报错时,前端因已设置responseType: 'blob'无法再接收json格式数据,会把json格式 ...
- centos7关于防火墙的一些操作
防火墙相关 # 检查防火墙状态 systemctl status firewalld # 开启防火墙 systemctl start firewalld # 关闭防火墙 systemctl stop ...
- Java单例-双重检查锁
问题引入 Java中实现单例模式,一般性的做法是如下方式: class Singleton { private static Singleton INSTANCE = null; private Si ...
- Dart简易教程 (1)---数据类型 运算符,类转换换
从下面开始学习DART编程 以下是一个简单的示例: main(){ var number = 42; print(number);}程序说明,dart是一个强大的脚本类语言,可以不预先定义变量类型 , ...
- C#动态构建表达式树(三)——表达式的组合
C#动态构建表达式树(三)--表达式的组合 前言 在筛选数据的过程中,可能会有这样的情况:有一些查询条件是公共的,但是根据具体的传入参数可能需要再额外增加一个条件.对于这种问题一般有两种方法: a. ...