zoukankan      html  css  js  c++  java
  • 1007 Maximum Subsequence Sum

    Given a sequence of K integers { N1​​, N2​​, ..., NK​​ }. A continuous subsequence is defined to be { Ni​​, Ni+1​​, ..., Nj​​ } where 1. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

    Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

    Input Specification:

    Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤). The second line contains K numbers, separated by a space.

    Output Specification:

    For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

    Sample Input:

    10
    -10 1 2 3 4 -5 -23 3 7 -21
    

    Sample Output:

    10 1 4
    我的代码
    #include <iostream>
    #include <algorithm>
    #include<cstring> 
    #define inf 10000
    using namespace std; 
    int n,arr[inf],s,e,l=0,r,sum=0;
    int sum1(int l,int r)
    {
        int sum=0;
        for(int i=l;i<=r;i++)
        {
             sum+=arr[i];
        }
        return sum;
    }
    int main()
    {
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
        }
        sum=sum1(0,0);
        for(int i=0;i<n;i++)
        {
            for(int j=i;j<n;j++)
            {
                if(sum1(i,j)>sum)
                {
                sum=sum1(i,j);
                s=arr[i];
                e=arr[j];
                }
            }
        }
        if(sum<0)
        cout<<0<<" "<<arr[0]<<" "<<arr[n-1]<<endl;
        else
        cout<<sum<<" "<<s<<" "<<e<<endl;
        return 0;
    }

    柳神代码

    #include <algorithm>
    #include<cstring> 
    #include<iostream>
    #define inf 10000
    using namespace std; 
    int main()
    {
        int n,arr[inf],l=0,r=0,sum=-1,dp[inf],temp=0,left=0;//
        cin>>n;
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
            temp+=arr[i];
            if(temp<0)
            {
                temp=0;
                left=i+1;
            }
            else if(temp>sum){
                sum=temp;
                l=left;
                r=i;
            }
        }
        if(sum<0)
        cout<<0<<" "<<arr[0]<<" "<<arr[n-1]<<endl;
        else
        cout<<sum<<" "<<arr[l]<<" "<<arr[r]<<endl;;
         return 0;
    }
    如果你够坚强够勇敢,你就能驾驭他们
  • 相关阅读:
    Friends ZOJ
    2^x mod n = 1 HDU
    Paint the Grid Reloaded ZOJ
    Treap 模板
    bzoj进度条
    。。。
    bzoj
    。。。
    bzoj
    题解continue
  • 原文地址:https://www.cnblogs.com/liuzhaojun/p/11141536.html
Copyright © 2011-2022 走看看