01-Complexity of 2. Maximum subsequence Sum (25)

Source: Internet
Author: User

Topic from the "Data structure problem" of NetEase cloud Classroom

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 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 K (<= 10000). The second line contains K 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 I and j (as shown by the Samp Le case). If all the K numbers was negative, then it maximum sum is defined to being 0, and you were supposed to output the first and T He last numbers of the whole sequence. Sample Input:

Ten
-10 1 2 3 4-5-23 3 7-21
Sample Output:
10 1 4

#include <stdio.h>

int main (void)
{
    int K, I, sum = 0, max = 0, begin, end, TMP;
    printf ("Please input K:");
    scanf ("%d", &k);
    int A;
    printf ("Please enter into K numbers, split by space:\n");
    for (i=0; i<k; ++i)
    {
        scanf ("%d", &a);
        if (i = = 0)
            tmp = A;
        if (Sum < 0)
        {
            sum = A;
            TMP = A;
        }
        else
        {
            sum + = A;
        }
        if (Sum > Max)
        {
            begin = tmp;
            end = A;
            max = sum;
        }
    }
    if (Max < 0)
        printf ("The Maximum subsequence sum:0, begin with%d, end with%d", begin, end);
    else
        printf ("The Maximum subsequence Sum:%d, begin with%d, end with%d", Max, begin, end);
    
    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.