• 最大子序列和(Max Sum ,Super Jumping! Jumping! Jumping! )


    连续的子序列:

                                                                                                  Max Sum

    Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.

    Input

    The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

    Output

    For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

    Sample Input

    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5

    Sample Output

    Case 1:
    14 1 4
    
    Case 2:
    7 1 6
    #include <cstdio>
    #include <iostream>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int T,e=1;
        cin>>T;
        while(T--)
        {
            int n,a[100010],i,j,k;
            cin>>n;
            for(i=1;i<=n;i++)
                cin>>a[i];
            int sum=0,maxx=-1e7;
            int beg=1,endd=1,t=1;
            for(i=1;i<=n;i++)
            {
                sum+=a[i];
                if(sum>maxx)
                {
                    maxx=sum;
                    beg=t;
                    endd=i;
                }
                if(sum<0)
                {
                    t=i+1;
                    sum=0;
                }
            }
            cout<<"Case "<<e++<<":"<<endl<<maxx<<" "<<beg<<" "<<endd<<endl;
            if(T>0)
                cout<<endl;
        }
        return 0;
    }

    不连续的子序列:

                                                       Super Jumping! Jumping! Jumping!

    Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.



    The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
    Your task is to output the maximum value according to the given chessmen list.

    Input

    Input contains multiple test cases. Each test case is described in a line as follow:
    N value_1 value_2 …value_N
    It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
    A test case starting with 0 terminates the input and this test case is not to be processed.

    Output

    For each case, print the maximum according to rules, and one line one case.

    Sample Input

    3 1 3 2
    4 1 2 3 4
    4 3 3 2 1
    0

    Sample Output

    4
    10
    3

    思路:

              就是求一个最长上升子序列的和,子序列不一定要相邻,这个动态规划可以先看前三项,dp[1] = num[1],这是num[2]如果大于num[1],dp[2] = dp[1] + num[2],否则dp[2] = num[2],在看第三个num[3],如果num[3]大于num[2]那么dp[3] = dp[2] + num[3],如果num[3]大于num[1],dp[3] = dp[1] + num[3],如果num[3]不大于num[2]同时也不大于num[1],dp[3] = num[3]。说了这么多其实就是一个意思,用之前的每一个数和当前的数比较,比当前数小的就加上dp【之前数】,不然就等于当前数。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstdlib>
    #include <cstring>
    using namespace std;
    typedef long long LL;
    LL dp[100001],a[100001];
    int main()
    {
    
        LL n;
        while(~scanf("%lld",&n)&&n)
        {
            LL i,j,k,maxx=-1e9;
            a[0]=0;
            for(i=1;i<=n;i++)
                cin>>a[i];
            memset(dp,0,sizeof(dp));
            for(i=1;i<=n;i++)
            {
                    for(j=1;j<=i;j++)
                        if(a[j]<a[i])
                            dp[i]=max(dp[j]+a[i],dp[i]);
    
                dp[i] = max(dp[i],a[i]);
                maxx=max(maxx,dp[i]);
    
            }
            cout<<maxx<<endl;
    
        }
        return 0;
    }
  • 相关阅读:
    历史博文汇总
    Golang快速入门:从菜鸟到大佬
    网络1911、1912 DS第4次作业--批改总结
    [机器学习实战-Logistic回归]使用Logistic回归预测各种实例
    [机器学习实战-kNN算法]约会网站问题和手写数字系统实例
    SQL学习笔记(一)之基础语句
    Python学习笔记(九)之面向对象编程(下)
    Python学习笔记(八)之面向对象编程(上)
    Python学习笔记(七)之Python模块
    Python学习笔记(六)之函数式编程
  • 原文地址:https://www.cnblogs.com/jk17211764/p/9677382.html
Copyright © 2020-2023  润新知