pat 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

Topical:
Typical and simple greedy algorithm. First, find the first positive number in the array, and assign this element to max, if traverse to the end of Array, print as the
topic has said. For each step of traversing, you need sum the elements from s, the possiable maximum sequence's start position, to the position of this step, if the sum
bigger than max, then renew the max, or if sum has alreay less than 0, assign 0 to sum and renew the possiable maximum sequence's start position to next step.

Code:
#include <iostream>
using namespace std;
int main()
{
    int k;
    int st, ed, s, e;
    int a[10000];
    int i;
    cin >> k;
    for (i = 0; i < k; i++)
    {
        cin >> a[i];
    }
    int max, count;
    i = 0;
    while (i < k && a[i] < 0)
        i++;
    if (i == k)
    {
        cout << 0 << " " << a[0] << " " << a[k - 1];
        return 0;
    }
    count = max = a[i];
    s = st = ed = i;
    for (i = i + 1; i < k; i++)
    {
        count += a[i];
        e = i;
        if (count > max)
        {
            max = count;
            st = s;
            ed = e;
        }
        else if (count < 0)
        {
            count = 0;
            s = e = i + 1;
        }
    }
    cout << max << ' ' << a[st] << ' ' << a[ed];
    return 0;
}

Impression:

  First, the reason i use English to write this blog is that my English postgraduate was defeated in the composition, so this is one part of my plan to solve this problem. As you can see, maybe now the use of vocabulary and grammer is not satisfactory, but i trust one day i will be good at writing. Second, the 1007 is my first step of greedy algorithm learning.




posted @ 2019-05-28 19:57  StormAX  阅读(147)  评论(0编辑  收藏  举报