CF1195C Basketball Exercise (dp + 贪心)

 

题意翻译

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

题目描述

Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. 2 \cdot n2n 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^51n105 ) — 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^91h1,i109 ), 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^91h2,i109 ), 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[i1][0],f[i1][1],f[i1][2]}f[i][1]=max\lbrace f[i-1][0],f[i-1][2]\rbrace+height[i][1]f[i][1]=max{f[i1][0],f[i1][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[i1][0],f[i1][1]}+height[i][2]

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

代码如下:

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

 

转载于:https://www.cnblogs.com/YY666/p/11238715.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值