zoukankan      html  css  js  c++  java
  • Basketball Exercise

    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:

    注意h可以是1e9,所以dp要是用int会溢出

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <unordered_set>
    #include <unordered_map>
    //#include <xfunctional>
    #define ll long long
    #define mod 998244353
    using namespace std;
    int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    
    int main()
    {
        int n;
        cin >> n;
        ll n1 = 2;
        vector<vector<ll>> h(2, vector<ll>(n + 1, 0)),dp(2, vector<ll>(n + 1, 0));
        for (int i = 0; i < 2; i++)
            for (int j = 1; j <= n; j++)
                cin >> h[i][j];
        for (int i = 1; i <= n; i++)
        {
            dp[0][i] = max(dp[1][i - 1] + h[0][i], dp[0][i - 1] - h[0][i - 1] + h[0][i]);
            dp[1][i] = max(dp[0][i - 1] + h[1][i], dp[1][i - 1] - h[1][i - 1] + h[1][i]);
        }
        cout << max(dp[0][n], dp[1][n]);
        return 0;
    }
  • 相关阅读:
    方法功能从无参方法、含参方法到重载方法
    文件目录在Visual C++ 2005中使用 GNU Scientific Library
    数据库导出All about Oracle IMP/EXP
    初始化变量明明白白c++ 初始化
    内容组织高效项目方案是如何产生的 !
    函数矩阵OpenGL中glFrustum()和gluPerspective()的相互转换
    对象数组[置顶] java高级工程师JSON和XML的使用
    资源成员函数Android应用程序资源的查找过程分析
    事件复制[wxWidgets编程系列2]_[发送异步事件的注意项之字符串深浅复制]
    查找删除Code First Entity Framework基本与最佳添加(add/create),删除(delete/remove),修改(update/modify)操作
  • 原文地址:https://www.cnblogs.com/dealer/p/12381613.html
Copyright © 2011-2022 走看看