PAT-1007. Maximum Subsequence Sum (25)

来源:互联网 发布:minecraft0.15js 编辑:程序博客网 时间:2024/06/06 05:55

Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1 <= i <= j <= K. 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 (<= 10000). 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>using namespace std;int maxSubseqSum(int a[],int n);int start,stop;int main(){    int i,k;    int a[10005];    cin>>k;    for(i=0;i<k;i++)        cin>>a[i];    cout<<maxSubseqSum(a,k)<<" ";    cout<<a[start]<<" "<<a[stop]<<endl;    return 0;}int maxSubseqSum(int a[],int n){    int i,flag=0;    int thisSum=0,maxSum=0;    for(i=0;i<n;i++)                                          //在该循环内判断序列中是否存在正数     {        if(a[i]>0)  flag=1;                                   //用flag标记,若存在正数则为1     }    if(flag==0)                                               //若不存在正数,则分为全负和有负有零两种     {        for(i=0;i<n;i++)                                      //若有负有零,例如:-1 -2 0 -3 0 -4。则输出应为:0 0 0。这里可以不用循环而直接设置输出,          {                                                     //但题目的本意肯定还是让找出第一个零的位置,所以用一个循环找到start和stop            if(a[i]==0){maxSum=0;start=i;stop=i;return 0;}        }        maxSum=0;start=0;stop=n-1;                             //若全为负则按照题意直接输出首尾元素         return 0;    }    for(i=0;i<n;i++)                                          //若序列中存在正数则按动归的思路求解子序列最大和maxSum     {        thisSum+=a[i];        if(thisSum<0)        {           thisSum=0;        }        else if(thisSum>maxSum)        {            maxSum=thisSum;stop=i;                            //确定最后一个元素的下标;         }       }                                                            thisSum=maxSum;        for(i=stop;i>=0;i--)                                  //由于子序列最大和为maxSum,所以只需将maxSum与前面各项相减到零即可确定第一个元素下标start         {                                                     //由于题目中规定若有不只一组最大子序列和时输出start和stop较小的那一组,所以i要减到0。                thisSum-=a[i];                                    //以防把前面相加等于零的子列忽略。若这里规定的是输出stop-start最小的一组,则再             if(thisSum==0) start=i;                           //判断thisSum==0时退出循环即可        }    return maxSum;}