CodeForces 762D Maximum path

http://codeforces.com/problemset/problem/762/D

因为是3*n很巧妙的地方是 往左走两步或更多的走法都可以用往回走以一步

并走完一列来替换 那么走的方法就大大减少 左边一列转移到右边一列 每个

格子的转移方法枚举出来 用动态规划即可解决

最主要的是因为他能够往回走.
但是我们画图可以发现:每次往回走一定不用超过1次.
也就是说,最多只能走成这样

而不会走成这样

因为下图的走法一定可以用上图组合,并且
由于只用3行的特性,每次向回走实际上是取走了所有的数.
所以我们只采用上图方式得出来的答案一定最优

 1 #include <bits/stdc++.h>
 2 #define INF 0x7fffffff
 3 using namespace std;
 4
 5 typedef long long LL;
 6 LL grid[3][100007];
 7 LL tmp[3][100007];
 8 LL dp[3][100007];
 9 int main()
10 {
11     int n;
12     scanf("%d", &n);
13     for (int i = 0; i < 3; i++)
14     for (int j = 0; j < n; j++)
15     {
16         scanf("%lld", &grid[i][j]);
17     }
18     dp[0][0] = grid[0][0];
19     dp[1][0] = grid[0][0] + grid[1][0];
20     dp[2][0] = grid[0][0] + grid[1][0] + grid[2][0];
21     tmp[0][0] = grid[0][0];
22     tmp[1][0] = grid[1][0];
23     tmp[2][0] = grid[2][0];
24     for(int j = 1;j < n; j++)
25     {
26         for (int i = 0; i < 3; i++)
27         {
28             dp[i][j] = tmp[i][j] = dp[i][j-1] + grid[i][j];
29         }//这样的转移走法 包括了所有的走法
30         dp[0][j] = max(dp[0][j], tmp[1][j] + grid[0][j]);
31         dp[0][j] = max(dp[0][j], tmp[2][j] + grid[1][j] + grid[0][j]);
32         dp[1][j] = max(dp[1][j], tmp[0][j] + grid[1][j]);
33         dp[1][j] = max(dp[1][j], tmp[2][j] + grid[1][j]);
34         dp[2][j] = max(dp[2][j], tmp[1][j] + grid[2][j]);
35         dp[2][j] = max(dp[2][j], tmp[0][j] + grid[1][j] + grid[2][j]);
36         dp[0][j] = max(dp[0][j], tmp[2][j-1] + grid[2][j] + grid[1][j] + grid[1][j-1] + grid[0][j-1] + grid[0][j]);
37         dp[2][j] = max(dp[2][j], tmp[0][j-1] + grid[0][j] + grid[1][j] + grid[1][j-1] + grid[2][j-1] + grid[2][j]);
38     }
39     cout << dp[2][n-1] << endl;
40     return 0;
41 }

dp[i][j] i 行 j 列可以得到的最大值

tmp[i][j]直接从右边一个走过来的得到的值

时间: 2024-09-30 06:35:17

CodeForces 762D Maximum path的相关文章

cf 762D. Maximum path

天呢,好神奇的一个DP23333%%%%% 因为1.向左走1格的话相当于当前列和向左走列全选 2.想做走超过1的话可以有上下走替代.而且只能在相邻行向左. 全选的情况只能从第1行和第3行转移,相反全选的情况也只能转移到第1行和第3行. (大雾,DP太玄乎了,不是很懂2333) 1 #include<bits/stdc++.h> 2 #define LL long long 3 #define N 100005 4 #define lowbit(x) x&(-x) 5 using nam

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1      / \     2   3 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

Binary Tree Maximum Path Sum 自底向上求解(重重重)

题目: 链接 解答: 自底向上求解.left_max right_max分别返回了左右子树的最大路径和,如果左右子树最大路径和小于0,那么返回零, 用这个最大路径和和根节点的值相加,来更新最大值,同时, 更新返回该树的最大路径值. 代码: class Solution { public: int max = INT_MIN; int maxPathSum(TreeNode *root) { if (root == NULL) return 0; search(root); return max;

第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节点为结尾(包含它或者不包含)的最大值,有两种情况,分别来自左儿子和右儿子设为Vnow. 然后考虑经过这个节点的情况来更新最终答案.更新答案后返回Vnow供父节点继续更新. 代码很简单. 有一个类似的很有趣的题目,给定一个二叉树,选择一条路径,使得权值最大的和最小的相差最大.参考POJ3728 cla

[LeetCode][Java] 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. For example: Given the below binary tree, 1 / 2 3 Return 6. 题意: 给定一棵二叉树,找出最大得路径和. 路径的起始和结束位置可以是树中的任意节点. 比如, 给定如下的一棵二叉树 1 / 2 3 返回  6. 算法分析: 1) Rec

LeetCode_Binary Tree Maximum Path Sum

一.题目 Binary Tree Maximum Path Sum Total Accepted: 41576 Total Submissions: 193606My Submissions Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Retu

[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. For example: Given the below binary tree, 1 / 2 3 Return 6. [分析]    需要考虑以上两种情况: 1 左子树或者右子树中存有最大路径和 不能和根节点形成一个路径 2 左子树 右子树 和根节点形成最大路径 [代码] /******

LeetCode124 Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. (Hard) For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does n

二叉树最大路径和-Binary Tree Maximum Path Sum

Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl