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 ...
随机推荐
- vertx从入门到精通
1.Vert.x安装指南 http://blog.csdn.net/sdyy321/article/details/38926005 http://blog.csdn.net/chszs/articl ...
- Jquery 错误提示插件
这是一个简单的输入框错误提示插件,可拓展! .jq-error{ font-size:12px; min-width:150px; width:auto; max-width:350px; line- ...
- aspose.cell 给excel表格设置样式
方法1: Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式 styleTitle.HorizontalAlignment ...
- Error:(3, 32) java: 程序包org.springframework.boot不存在
解决方案一: 找同事传一份D:\maven_repository\org\springframework\boot ,如图所示的位置,添加进去立刻就不报红.我也可以给你发.... 解决方案二: ...
- url编码和解码平台
http://meyerweb.com/eric/tools/dencoder/
- CPP-基础:wchar_t
目 录 1简介 2例如 3将char转换成wchar_t 1.简介 wchar_t是C/C++的字符数据类型,是一种扩展的字符存储方式,wchar_t类型主要用在国际化程序的实现中,但它不等同于uni ...
- 数据库-SQL语法:GROUP BY与HAVING
注意:select 后的字段,必须要么包含在group by中,要么包含在having 后的聚合函数里. 1. GROUP BY 是分组查询, 一般 GROUP BY 是和聚合函数配合使用. grou ...
- Vue-Quill-Editor 富文本编辑器的使用
步骤如下: 1.下载Vue-Quill-Editor npm install vue-quill-editor --save 2.下载quill(Vue-Quill-Editor需要依赖) npm i ...
- 【数位dp】bzoj1833: [ZJOI2010]count 数字计数
数位dp姿势一直很差啊:顺便庆祝一下1A Description 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. Input 输入文件中仅包含一行两个整数a ...
- Objective-C 正则表达式使用(1)
学习了一下OC的正则表达式备忘一下 使用正则表达式的步骤: 创建一个一个正则表达式对象:定义规则. 利用正则表达式对象测试,相应的字符串. NSString *userName = @"12 ...