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

时间: 2024-08-01 06:50:21

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

codeforces219C - Color Stripe DP+贪心

题意:给你n,k,大意就是说给你一个已经涂满颜色长为n的字符串,现有k种颜色可以选择,问你最少要改变多少个箱子的颜色使得相邻箱子之间颜色不同. 解题思路:当k = 2 时单独讨论,不能用贪心,其余情况都可贪心得到. 解题代码: 1 // File Name: 219c.cpp 2 // Author: darkdream 3 // Created Time: 2014年07月26日 星期六 15时45分56秒 4 5 #include<vector> 6 #include<list>

ZOJ 2109 FatMouse&#39; Trade (背包 dp + 贪心)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1109 FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J

UVALive 4731 dp+贪心

这个题首先要利用题目的特性,先贪心,否则无法进行DP 因为求期望的话,越后面的乘的越大,所以为了得到最小值,应该把概率值降序排序,把大的数跟小的系数相乘 然后这种dp的特性就是转移的时候,由 i推到i+1每次添加一个数,就要考虑这个新数应该和谁放在一组,枚举他放在哪一组即可 dp[i][j]代表当前第i个数有j个分组时候的最小值 dp[i][j]=dp[k][j-1]+i(prefix[i]-prefix[k-1]),k代表枚举第几个数开始和当前新添加的数为一组,prefix为前缀和,为了迅速得

HDU 5303(Delicious Apples- 环上折半dp+贪心)

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Total Submission(s): 1585    Accepted Submission(s): 514 Problem Description There are n apple trees planted along a cyclic road, which is L metres

Codeforces 459E Pashmak and Graph(dp+贪心)

题目链接:Codeforces 459E Pashmak and Graph 题目大意:给定一张有向图,每条边有它的权值,要求选定一条路线,保证所经过的边权值严格递增,输出最长路径. 解题思路:将边按照权值排序,每次将相同权值的边同时加入,维护每个点作为终止点的最大长度即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 3

Codeforces 77C 树形dp + 贪心

题目链接:点击打开链接 题意: 给定n个点, 每个点的豆子数量 下面是一棵树 再给出起点 每走到一个点,就会把那个点的豆子吃掉一颗. 问:回到起点最多能吃掉多少颗豆子 思路:树形dp 对于当前节点u,先把子节点v都走一次. 然后再往返于(u,v) 之间,直到u点没有豆子或者v点没有豆子. dp[u] 表示u点的最大值.a[u] 是u点剩下的豆子数. #include <cstdio> #include <vector> #include <algorithm> #inc

uva 1534 - Taekwondo(dp+贪心)

题目连接:uva 1534 - Taekwondo 题目大意:有两组什么东西,题目背景有点忘记了,就是给出两组数,两组个数分别为n,m,要求找出min(n,m)对数,每个数最多最多选一次,使得这min(n,m)对数ai,bi,ai-bi的绝对值之和最小. 解题思路:贪心,将两组数分别排序,然后dp[i][j]表示i对,匹配到j时候的最优解. #include <cstdio> #include <cstring> #include <cmath> #include &l

Codeforces 830A. Office Keys (背包dp+贪心) / (二分+贪心)

题目链接: http://codeforces.com/problemset/problem/830/A 题意: n个人,k个钥匙(n<=k),p表示这些人要到达的位置 给出n个人的位置以及钥匙的位置,问花时间最多的那个人用时最少是多少?? 思路: 二分+贪心: 二分最少时间,需要对a,b位置数组排序,我们check函数只需要从左到右一个一个找过去,因为如果选后边的点,可能会使结果更差,假如当前这个人选后面的点,那可能会选中后面的人可以选的唯一的钥匙,不会使解更优. check(40)的时候答案

HDU 4939 Stupid Tower Defense(dp+贪心)

dp[i][j]表示到了第i步放了j个减速,造成的伤害.我们用贪心的策略把造成一段伤害的放在最后面,造成持续伤害的与减速放在前i个中这样得到的伤害是最高的. 所以前(i,j)中的伤害为dp[i][j] = max(dp[i-1][j]+(j*z+t)*(max(0LL, i-1-j))*y, dp[i-1][j-1]+((j-1)*z+t)*(i-j)*y); 每次造成的伤害就为:dp[i][j]+(n-i)*(j*z+t)*(x+(i-j)*y).然后取一个最大值,就是伤害的最大值了. Stu