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;
    }
    如果你够坚强够勇敢,你就能驾驭他们
  • 相关阅读:
    SQL Server 2008 官方简体中文正式版【附开发版和企业版序列号】
    安装SQL 2008 重启之后 一再提示重启计算机问题
    wp7使用C#通过后台动态生成Grid网格布局
    wp7中空格的编码
    vs.php 2.10 for 2010 注册码
    hive 优化
    Remove '@Override' annotation错误
    ucfirst
    Hadoop错误
    git patch 转帖
  • 原文地址:https://www.cnblogs.com/liuzhaojun/p/11141536.html
Copyright © 2011-2022 走看看