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的更多相关文章

  1. Codeforces 762D Maximum path 动态规划

    Codeforces 762D 题目大意: 给定一个\(3*n(n \leq 10^5)\)的矩形,从左上角出发到右下角,规定每个格子只能经过一遍.经过一个格子会获得格子中的权值.每个格子的权值\(a ...

  2. cf 762D. Maximum path

    天呢,好神奇的一个DP23333%%%%% 因为1.向左走1格的话相当于当前列和向左走列全选 2.想做走超过1的话可以有上下走替代.而且只能在相邻行向左. 全选的情况只能从第1行和第3行转移,相反全选 ...

  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. ...

  4. [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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 【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 ...

随机推荐

  1. ios invalid put policy encoding 七牛上传报错

    获取七牛token的时候deadline不能为NSString类型 NSDictionary *infoDic = @{@"scope":@"yangtao", ...

  2. 添加 SSH 公钥

    生成 SSH 密钥 ssh-keygen -t rsa -C "YOUR_EMAIL@YOUREMAIL.COM" 获取 SSH 公钥信息 cat ~/.ssh/id_rsa.pu ...

  3. CentOS7——防火墙设置

    1.查看firewall服务状态 systemctl status firewalld 2.查看firewall的状态firewall-cmd --state 3.开启.重启.关闭.firewalld ...

  4. Python 输入输出 数据类型 变量

    python输入输出 数据类型 变量 输入输出 print()在Python3中是函数 >>>print('hello world') #print注意print前面不要有任何空格 ...

  5. EF关于报错Self referencing loop detected with type的原因以及解决办法

    1)具体报错 { "Message": "出现错误.", "ExceptionMessage": "“ObjectContent` ...

  6. 解决微信小程序要求的TLS版本必须大于等于1.2的问题(windows2008服务器)

    开始->运行->输入 PowerShell 复制这段代码粘入弹出的dos窗口内 # Enables TLS 1.2 on windows Server 2008 R2 and Window ...

  7. Jarvis OJ-Level4

    借助DynELF实现无libc的漏洞利用小结 #!/usr/bin/env python # coding:utf-8 from pwn import * elf = ELF('level4') wr ...

  8. 【求助】NdisSend,自定义数据包发送失败?

    做ndis hook的时候,自定义了一个数据包,包结构应该没有问题,填充NDIS_PACKET结构是这样的,先初始化:        NdisAllocatePacketPool(&nStat ...

  9. 同时使用多个UITableView

    1.xib\storyboard中给2个tableView设置constraints(等宽) 方法 : ①设置mainTableView的上\下\左\三部分的约束为0:subTableView上\下\ ...

  10. logstash-基础操作

    一.环境 https://www.cnblogs.com/suffergtf/p/9566145.html 1.收取日志文件到elasticsearch [root@linux-node1 logst ...