zoukankan      html  css  js  c++  java
  • C. Basketball Exercise dp

    C. Basketball Exercise

    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. 2n2⋅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 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 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.

    Input

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

    The second line of the input contains nn integers h1,1,h1,2,,h1,nh1,1,h1,2,…,h1,n (1h1,i1091≤h1,i≤109), where h1,ih1,i is the height of the ii-th student in the first row.

    The third line of the input contains nn integers h2,1,h2,2,,h2,nh2,1,h2,2,…,h2,n (1h2,i1091≤h2,i≤109), where h2,ih2,i is the height of the ii-th student in the second row.

    Output

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

    Examples
    input
    Copy
    5
    9 3 5 7 3
    5 8 1 4 5
    
    output
    Copy
    29
    
    input
    Copy
    3
    1 2 9
    10 1 1
    
    output
    Copy
    19
    
    input
    Copy
    1
    7
    4
    
    output
    Copy
    7
    
    Note

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

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

    题意:每行输入n个数,一共两行,取过一个数之后,不能再取和它相邻的数(不包括对角相邻),问取数总和最大为多少

    题解:状态转移方程:dp[0][i]=max( dp[0][i] , dp[1][i-1]+a[i] );   

                                        dp[1][i]=max( dp[1][i] , dp[0][i-1]+b[i] );

     

    #include<iostream>
    #include<algorithm>
    #include<math.h>
    #define ll long long
    using namespace std;
    ll dp[2][100005],a[100005],b[100005];
    ll n;
    int main()
    {
        ll n;
        cin>>n;
        for(int i=0;i<n;i++)
            scanf("%lld",&a[i]);
        for(int i=0;i<n;i++)
            scanf("%lld",&b[i]);
        dp[0][0]=a[0];
        dp[1][0]=b[0];
        for(int i=1;i<n;i++)
        {
            dp[0][i]=max(dp[0][i-1],dp[1][i-1]+a[i]);
            dp[1][i]=max(dp[1][i-1],dp[0][i-1]+b[i]);
        }
        ll ans=max(dp[0][n-1],dp[1][n-1]);
        printf("%lld
    ",ans );
        return 0;
    }

     

     

     

     

     

  • 相关阅读:
    Linux内核设计第三周学习总结 跟踪分析Linux内核的启动过程
    Linux内核设计第二周学习总结 完成一个简单的时间片轮转多道程序内核代码
    Linux内核设计第一周学习总结 计算机如何工作
    信息安全系统设计基础期末总结
    信息安全系统设计基础第十四周学习总结
    信息安全系统设计基础第十三周学习总结
    20135310陈巧然 20135305姚歌 实验四:外设驱动程序设计
    linux内核设计与实现一书阅读整理 之第一二章整合
    20135239 益西拉姆 linux内核分析 使用库函数API和C代码中嵌入汇编代码两种方式使用同一个系统调用
    20135239 益西拉姆 linux内核分析 跟踪分析Linux内核的启动过程
  • 原文地址:https://www.cnblogs.com/-citywall123/p/11207978.html
Copyright © 2011-2022 走看看