题意翻译

给定一个 2×n 的矩阵,现从中选择若干数,且任意两个数不上下或左右相邻,求这些数的和最大是多少?

题目描述

Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. 2 \cdot n2⋅n students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly nn people in each row). Students are numbered from 11 to nn in each row in order from left to right.

Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all 2n2n students (there are no additional constraints), and a team can consist of any number of students.

Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose.

输入输出格式

输入格式:

The first line of the input contains a single integer nn ( 1 \le n \le 10^51≤n≤105 ) — the number of students in each row.

The second line of the input contains nn integers h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}h1,1​,h1,2​,…,h1,n​ ( 1 \le h_{1, i} \le 10^91≤h1,i​≤109 ), where h_{1, i}h1,i​ is the height of the ii -th student in the first row.

The third line of the input contains nn integers h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}h2,1​,h2,2​,…,h2,n​ ( 1 \le h_{2, i} \le 10^91≤h2,i​≤109 ), where h_{2, i}h2,i​ is the height of the ii -th student in the second row.

输出格式:

Print a single integer — the maximum possible total height of players in a team Demid can choose.

输入输出样例

输入样例#1:

5
9 3 5 7 3
5 8 1 4 5
输出样例#1:

29
输入样例#2:

3
1 2 9
10 1 1
输出样例#2:

19
输入样例#3:

1
7
4
输出样例#3:

7

说明

In the first example Demid can choose the following team as follows:

In the second example Demid can choose the following team as follows:

题解出处:https://www.luogu.org/problemnew/solution/CF1195C

很水的一道C题……目测难度在黄~绿左右。请各位切题者合理评分。

注意到可以选择的球员编号是严格递增的,因此可以把状态的第一维定义为球员编号,第二维描述编号同为 ii 的两名球员的选取情况。

定义状态:f[i][0/1/2]f[i][0/1/2] 表示选取了编号在 ii 及以前的球员,所能得到的身高总和最大值。其中,第二维的 00 表示编号为 ii 的球员一个都不选;11 表示只选上面一个;ii 表示只选下面一个。(显然没有上下都选的情况)

状态转移方程:

f[i][0]=max{f[i−1][0],f[i−1][1],f[i−1][2]}f[i][1]=max\lbrace f[i-1][0],f[i-1][2]\rbrace+height[i][1]f[i][1]=max{f[i−1][0],f[i−1][2]}+height[i][1]f[i][2]=max\lbrace f[i-1][0],f[i-1][1]\rbrace+height[i][2]f[i][2]=max{f[i−1][0],f[i−1][1]}+height[i][2]

Update: 用贪心可以证明,在最优解中,不会出现连续两列一个不取的情况。因此, f[i][0]f[i][0] 其实没有必要考虑来自 f[i-1][0]f[i−1][0] 的状态转移。

代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
ll h[][];
ll f[][];
int main() {
cin >> N;
for (register int i = ; i <= N; ++i) cin >> h[i][];
for (register int i = ; i <= N; ++i) cin >> h[i][];
f[][] = ;
f[][] = h[][];
f[][] = h[][];
for (register int i = ; i <= N; ++i) {
f[i][] = max(f[i - ][], max(f[i - ][], f[i - ][]));
f[i][] = max(f[i - ][], f[i - ][]) + h[i][];
f[i][] = max(f[i - ][], f[i - ][]) + h[i][];
}
cout << max(f[N][], max(f[N][], f[N][]));
return ;
}

CF1195C Basketball Exercise (dp + 贪心)的更多相关文章

  1. C. Basketball Exercise dp

    C. Basketball Exercise time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. Codeforces Round #304 (Div. 2) C. Basketball Exercise (DP)

    题意:给你两个长度相同的数组,每次从两个数组中选数(也可以不选),但是不可以在同一个数组中连续选两次,问能选的最大值是多少? 题解:dp,\(dp[i][0]\)表示第\(i\)个位置不选,\(dp[ ...

  3. 【bzoj4027】[HEOI2015]兔子与樱花 树形dp+贪心

    题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接,我们可以把它 ...

  4. BZOJ 2021 [Usaco2010 Jan]Cheese Towers:dp + 贪心

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2021 题意: John要建一个奶酪塔,高度最大为m. 他有n种奶酪.第i种高度为h[i]( ...

  5. 洛谷P2507 [SCOI2008]配对 题解(dp+贪心)

    洛谷P2507 [SCOI2008]配对 题解(dp+贪心) 标签:题解 阅读体验:https://zybuluo.com/Junlier/note/1299251 链接题目地址:洛谷P2507 [S ...

  6. Codeforces Round #574 (Div. 2)——C. Basketball Exercise(简单DP)

    题目传送门 题意: 输入n,给出两组均为 n个数字的数组a和b,轮流从a和b数组中取出一个数字,要求严格按照当前所选数字的数组下标比上一个所选数字的数组下标更大,计算能够取出的数字加起来的总和最大能为 ...

  7. 【BZOJ-1046】上升序列 DP + 贪心

    1046: [HAOI2007]上升序列 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3723  Solved: 1271[Submit][Stat ...

  8. Codeforces 675E Trains and Statistic(DP + 贪心 + 线段树)

    题目大概说有n(<=10W)个车站,每个车站i卖到车站i+1...a[i]的票,p[i][j]表示从车站i到车站j所需买的最少车票数,求所有的p[i][j](i<j)的和. 好难,不会写. ...

  9. 【HDU 2546】饭卡(DP+贪心)

    贪心:最贵的留到最后买.状态转移方程:dp[j]=dp[j+a[i]]|dp[j],dp[i]表示余下i元. 原来就不足5元,那就不能买啦. #include<cstdio> #inclu ...

随机推荐

  1. 京东sdk商家上架接口调用问题总结

    前言: 最近在做商家发布产品,调用京东sdk,发现问题很多,而且还是在我同事的帮助下完成的,摸索中,菜鸟还请高手门多多提携才好,入正题 首先是引用jd的sdk啦,京东sdk中发布商品需要调用一个 36 ...

  2. 初探 C# 8 的 Nullable Reference Types

    溫馨提醒:本文提及的 C# 8 新功能雖已通過提案,但不代表將來 C# 8 正式發布時一定會納入.這表示我這篇筆記有可能白寫了,也表示您不必急著瞭解這項新功能的所有細節,可能只要瞄一下底下的「概要」說 ...

  3. 使用Func<T1, T2, TResult> 委托返回匿名对象

    Func<T1, T2, TResult> 委托 封装一个具有两个参数并返回 TResult 参数指定的类型值的方法. 语法 public delegate TResult Func< ...

  4. java-mysql(2) Prepared statement

    上一篇学习了java如何链接配置mysql,这篇学习下java如何处理sql预处理语句(PreparedStatement),首先是一个sql预处理的例子: package core; import ...

  5. 【算法随记三】小半径中值模糊的急速实现(16MB图7.5ms实现) + Photoshop中蒙尘和划痕算法解读。

    在本人的博客里,分享了有关中值模糊的O(1)算法,详见:任意半径中值滤波(扩展至百分比滤波器)O(1)时间复杂度算法的原理.实现及效果 ,这里的算法的执行时间和参数是无关的.整体来说,虽然速度也很快, ...

  6. WebGL场景的两种地面构造方法

    总述:大部分3D编程都涉及到地面元素,在场景中我们使用地面作为其他物体的承载基础,同时也用地面限制场景使用者的移动范围,还可以在通过设置地块的属性为场景的不同位置设置对应的计算规则.本文在WebGL平 ...

  7. Zookeeper详解-工作流和leader选举(三)

    一.工作流 一旦ZooKeeper集合启动,它将等待客户端连接.客户端将连接到ZooKeeper集合中的一个节点.它可以是leader或follower节点.一旦客户端被连接,节点将向特定客户端分配会 ...

  8. 【设计模式】行为型10中介者模式(Mediator Pattern)

    中介者模式(Mediator Pattern)     这里笔者完全参考了:http://www.runoob.com/design-pattern/mediator-pattern.html,案例精 ...

  9. kubernetes实战篇之helm填坑与基本命令

    系列目录 其实前面安装部分我们已经分享一些互联网上其它网友分享的一些坑,本篇介绍helm的基本使用以及在使用过程中碰到的一些坑. 客户端版本和服务端版本不一致问题 有些朋友可能在使用helm init ...

  10. html、javascript、url特殊字符的转义诠释及使用方法详解

    html.javascript.url特殊字符转义在实际编程中都是有用到的,有的人对特殊字符转义的使用不是很清楚,下面就对html,javascript,url特殊字符的转义做一下说明和归纳. htm ...