CodeForces-1195C Basketball Exercise(线性DP)


传送门:CodeForces - 1195C

题目描述

Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. 2⋅n students have come to Demid’s exercise session, and he lined up them into two rows of the same size (there are exactly n people in each row). Students are numbered from 1 to n 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 taken) 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 2n 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 $n$ $(1≤n≤10^5)$ — the number of students in each row.

The second line of the input contains n integers h1,1,h1,2,…,h1,n $(1≤h1,i≤10^9)$, where h1,i is the height of the i-th student in the first row.

The third line of the input contains n integers h2,1,h2,2,…,h2,n $(1≤h2,i≤10^9)$, where h2,i is the height of the i-th student in the second row.

输出描述

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

题意分析

给定两个序列a和b,可以选任意的数,但要满足以下规则:连续选择的两个数不能来自同一行同一列,求和最大值。

样例输入

5
9 3 5 7 3
5 8 1 4 5

样例输出

29

AC代码

#include<bits/stdc++.h>
#pragma GCC optimize(2)

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define ull unsigned long long
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);

const int maxn=1e6+10;
const ll mod=1e9+7;

ll a[maxn],b[maxn],dp[maxn][3];

int main() {
    IOS;
    ll n,i;
    cin>>n;
    for(i=1; i<=n; i++)cin>>a[i];
    for(i=1; i<=n; i++)cin>>b[i];
    for(i=1;i<=n;i++){
        dp[i][0]=max(dp[i-1][0],dp[i-1][1]+a[i]);
        dp[i][1]=max(dp[i-1][1],dp[i-1][0]+b[i]);
    }
    cout<<max(dp[n][0],dp[n][1])<<endl;
}

文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}