CodeForces 762D Maximum path
http://codeforces.com/problemset/problem/762/D
因为是3*n很巧妙的地方是 往左走两步或更多的走法都可以用往回走以一步
并走完一列来替换 那么走的方法就大大减少 左边一列转移到右边一列 每个
格子的转移方法枚举出来 用动态规划即可解决
最主要的是因为他能够往回走.
但是我们画图可以发现:每次往回走一定不用超过1次.
也就是说,最多只能走成这样
而不会走成这样
因为下图的走法一定可以用上图组合,并且
由于只用3行的特性,每次向回走实际上是取走了所有的数.
所以我们只采用上图方式得出来的答案一定最优
#include <bits/stdc++.h>
#define INF 0x7fffffff
using namespace std; typedef long long LL;
LL grid[][];
LL tmp[][];
LL dp[][];
int main()
{
int n;
scanf("%d", &n);
for (int i = ; i < ; i++)
for (int j = ; j < n; j++)
{
scanf("%lld", &grid[i][j]);
}
dp[][] = grid[][];
dp[][] = grid[][] + grid[][];
dp[][] = grid[][] + grid[][] + grid[][];
tmp[][] = grid[][];
tmp[][] = grid[][];
tmp[][] = grid[][];
for(int j = ;j < n; j++)
{
for (int i = ; i < ; i++)
{
dp[i][j] = tmp[i][j] = dp[i][j-] + grid[i][j];
}//这样的转移走法 包括了所有的走法
dp[][j] = max(dp[][j], tmp[][j] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j] + grid[][j] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j] + grid[][j] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j-] + grid[][j] + grid[][j] + grid[][j-] + grid[][j-] + grid[][j]);
dp[][j] = max(dp[][j], tmp[][j-] + grid[][j] + grid[][j] + grid[][j-] + grid[][j-] + grid[][j]);
}
cout << dp[][n-] << endl;
return ;
}
dp[i][j] i 行 j 列可以得到的最大值
tmp[i][j]直接从右边一个走过来的得到的值
CodeForces 762D Maximum path的更多相关文章
- Codeforces 762D Maximum path 动态规划
Codeforces 762D 题目大意: 给定一个\(3*n(n \leq 10^5)\)的矩形,从左上角出发到右下角,规定每个格子只能经过一遍.经过一个格子会获得格子中的权值.每个格子的权值\(a ...
- cf 762D. Maximum path
天呢,好神奇的一个DP23333%%%%% 因为1.向左走1格的话相当于当前列和向左走列全选 2.想做走超过1的话可以有上下走替代.而且只能在相邻行向左. 全选的情况只能从第1行和第3行转移,相反全选 ...
- [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 ...
随机推荐
- UVA1663 Purifying Machine (匈牙利算法,二分图最大匹配)
模版集合个数减少是因为匹配串集合中没被匹配过的一对串匹配了.所以就是找一个二分图最大匹配. 因为集合X和Y是不好分开的,但是可以直接跑,两个集合都会跑一遍,所以一个匹配会被算两次,返回的时候除以2就行 ...
- Higher level thinking
「Higher level thinking」-- 出自 Ray Dalio 的<Principles>(PDF 原文:Principles by Ray Dalio) Higher le ...
- 分布式文件系统ceph介绍
ceph哲学思想 1. 每个组件必须支持扩展 2.不存在单点故障 3.解决方案必须是基于软件的.开源的.适应能力强 4.任何可能的一切必须自我管理 存在的意义:帮助企业摆脱昂贵的专属硬件 ceph目标 ...
- 使用crontab定时执行python文件问题追根溯源
使用crontab执行定时任务不是第一次用,昨天下午设置几个任务,yy里面已存在的任务,修改指定python环境和执行文件路径后,死活到点不执行. 任务设置如下: 15 16 * * * /root/ ...
- 用python编写九九乘法表
for i in range(1,10): for j in range(1,10): if j >i: print(end='') else: print(j,'*',i,'=',i*j,en ...
- python_111_动态导入模块
lib下aa.py文件内容: class C: def __init__(self): self.name='alex' from lib import aa#正常导入 print(aa.C) 动态导 ...
- Webpack的使用指南-Webpack小结
参考文章: https://baijiahao.baidu.com/s?id=1594972657801970108&wfr=spider&for=pc 使用Webpack有一段时间了 ...
- LeetCode || 大杂烩w
454. 4Sum II 题意:给四个数组,每个数组内取一个数使得四个数和为0,问有多少种取法 思路:枚举为On4,考虑两个数组,On2枚举所有可能的和,将和的出现次数存入map中,On2枚举另两个数 ...
- tomcat性能优化 - 网络抄录
tomcat默认参数是为开发环境制定,而非适合生产环境,尤其是内存和线程的配置,默认都很低,容易成为性能瓶颈. tomcat内存优化 linux修改TOMCAT_HOME/bin/catalina.s ...
- Bootstrap历练实例:小的按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...