01-Complexity 2 Maximum subsequence Sum (25 min)

Source: Internet
Author: User
01-Complexity 2 Maximum subsequence Sum (25 min)

Given a sequence of KK integers {n1n 1, n2n 2, ..., Nkn K}. A continuous subsequence is defined to be {NiN I, ni1n i+1, ..., Njn J} where 1ijk1≤i≤j≤k. The Maximum subsequence is the continuous subsequence which have the largest sum of its elements. For example, given sequence {-2, one, -4, -5, 2}, its maximum subsequence are {One, -4, all} with the largest sum bei ng 20.

Now is supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence. Input Specification:

Each input file contains the one test case. Each case occupies the lines. The first line contains a positive integer KK (10000≤10000).  The second line contains KK numbers, separated by a space. Output Specification:

For each test case, output on one line the largest sum, together with the first and the last numbers of the maximum Subseq Uence. The numbers must is separated by one space, but there must is no extra space at the end of a line. In case the maximum subsequence are not unique, output the one with the smallest indices II and JJ (as shown by the SA Mple case). If all the KK numbers was negative, then its maximum sum was defined to being 0, and you were supposed to output the first and  The last numbers of the whole sequence. Sample Input:

Ten
-10 1 2 3 4-5-23 3 7-21
Sample Output:
10 1 4
Main ideas:
1, on-line processing to seek maximum sub-columns
2. Record the first and last nodes of the current and largest sub-columns while online processing
3, after the sum<0 to record may appear and the largest sub-column of the first node
#include <iostream> #include <vector> using namespace std;
    int main (int argc, const char * argv[]) {//input int K;
    cin>>k;
    Vector<int> VEC;
    int value;
        for (int i=0; i<k; ++i) {cin>>value;
    Vec.push_back (value);
    }//process int first=0,last=0,sum=0,max=-1; int Flag_max=0;//flag represent whether Max has been rewrite int flag=0;//sum 01 times, it is possible that subsequent sub-columns will appear greater than the current max of the series int Maybe_
        first=0;//may become the first node of the largest child columns for (int i=0; i<vec.size (); ++i) {sum+=vec[i];
            if (Sum>max)//update maximum child columns and {flag_max=1;
                if (flag==0)//sum is updated to 0, the first node {first=maybe_first) of the largest child columns is updated again after the largest child columns that is greater than the previous max;
            flag=1;
            } max=sum;
        last=i;//each occurrence of the new Maximum child column and the last node to update the child column}else if (sum<0)//sum appears less than 0, if the subsequent child column appears with a larger child columns, then the first node of the child column is saved as Maybe_first
            {maybe_first=i+1;
    sum=0;        flag=0; }}//output if (flag_max==0) {cout<< "0" << "<<vec[0]<<" <<ve
    C[vec.size ()-1];
    }else {cout<<max<< ' <<vec[first]<< ' <<vec[last];
} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.