HDU-1231 - maximal continuous subsequence - algorithm note

Keywords: Programming PHP less

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1231
Problem Description:

Given the sequence of K integers {N1, N2 , NK}, any continuous subsequence of which can be expressed as {Ni, Ni+1 ,
Nj}, where 1 < = I < = J < = K. The largest continuous subsequence is the element sum of all the continuous subsequences,
For example, given the sequence {- 2, 11, - 4, 13, - 5, - 2}, the largest continuous subsequence is {11, - 4, 13}, the largest sum
It is 20.
In this year's data structure test paper, it is required to write the program to get the maximum sum. Now, it is required to output the
The first and last elements of a subsequence.

Input:

Test input includes several test cases, each test case takes up 2 lines, the first line gives a positive integer k (< 10000), the second line gives K integers, separated by spaces. When k is 0, the input ends and the use case is not processed.

Output:

For each test case, output the first and last element of the maximum sum and maximum continuous subsequence in line 1
Element, separated by spaces. If the largest continuous subsequence is not unique, the one with the smallest output sequence numbers i and j (such as the second and third groups of input samples) will be output. If all K elements are negative, the maximum sum is defined as 0, and the first and last elements of the whole sequence are output.

Sample Input:
Sample Output:

First, determine the algorithm of the maximum continuous subsegment sum. Here I use the dynamic programming to see the idea:
                         .
Reference code:

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;

#define ll long long
#define clean(arrays) memset(arrays, 0, sizeof(arrays))

int s[10005];
int first, finally;

//dynamic programming
int MaxSum(int *a, int n)
{
    int ans;
    int sum = 0;        //Current maximum continuous fields and
    int index = 0;      //When saving the i-th number of decisions, the status of the preceding fields and
    for (int i = 1; i <= n; i++)
    {
        if (index <= 0)     //Previous fields and results are less than or equal to 0
        {
            index = a[i];   //Then count from the i th number
            ans = i;        //The ans here is used to record the starting value of the largest sub segment
        }
        else
        {
            index += a[i];  //If the result of the sum of the preceding subparagraphs is greater than 0, then add the ith number
        }
        if (index > sum)    //If the decision result is larger than sum, refresh sum
        {
            sum = index;
            finally = i;    //Record termination subscript
            first = ans;    //Record the starting subscript of the largest sub segment if no larger value than sum appears. In this way, the starting sequence with the smallest sequence number is selected.
        }
    }
    return sum;
}


int main()
{
    int n;
    while (cin>>n, n != 0)
    {
        first = 0;  finally = 0;
        for (int i = 1; i <= n; i++)
             cin>>s[i];
        int maxn = *max_element(s + 1, s + n);      //Find out the maximum value in the array and judge whether the whole array is negative
        if (maxn < 0)
            cout<<0<<" "<<s[1]<<" "<<s[n]<<endl;
        else
        {
            int index = MaxSum(s, n);
            cout<<index<<" "<<s[first]<<" "<<s[finally]<<endl;
        }

    }
}

Posted by nick2005 on Mon, 04 Nov 2019 08:50:20 -0800