HDU 1231 maximum continuous sequences (dynamic programming)

The largest contiguous subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43843    Accepted Submission(s): 20002

 

Problem Description

 

Given a sequence of K integers {N1, N2, ..., NK }, any of which can be expressed as contiguous subsequence of Ni {, of Ni +. 1, ...,
Nj}, where 1 <= i <= j < = K. Maximum continuous sequence is contiguous subsequence of all elements and a maximum,
for example, a given sequence {-2, 11 -4, 13, -5, -2}, which is the maximum continuous subsequence {11, -4, 13}, and the maximum
20.
In this year's data structure in the papers, and requires programming to get the maximum increase is now a requirement that also need to output the
first and the last element sequence.

 


Input

 

Test input comprising a plurality of test cases, each test representing line 2, line 1 gives a positive integer K (<10000), line 2 gives the K integers, separated by a space. When K is 0, the end of the input, which is not treated cases.

 


Output

 

For each test, the maximum output in line 1 and the first and last element of the maximum continuous sequence
element, separated by a space. If the maximum contiguous subsequence is not unique, the output number i and j, the smallest one (e.g., the input sample groups 2 and 3). If all elements K are negative, and defines the maximum is 0, the output of the entire sequence of elements inclusive.

 


6
-2 11 -4 13 -5 -2
10
-10 1 2 3 4 -5 -23 3 7 -21
6
5 -8 3 2 5 0
1
10
3
-1 -5 -2
3
-1 0 -2
0

Sample Output

20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0

Hint
Hint
 
Huge input, scanf is recommended.

Topic analysis

DP array need not even, only a few records and to sum successive current sequence, if sum <0, represents the current sequence no significance for the subsequent elements, updated sum, and then determines whether the maximum value, If it is the maximum update interval index to record. Note that the questions asked: if all elements K are negative, and defines the maximum is 0, the output of the entire sequence of elements inclusive.

Code

#include<bits/stdc++.h>

using namespace std;

int anss,sum,l,r,anssl,anssr,i,n,a[10005];

int main()
{
    while(scanf("%d",&n),n!=0)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        sum=a[1];
        l=1;
        r=1;
        anssl=1;
        anssr=1;
        anss=a[1];
        for(i=2;i<=n;i++)
        {
            if(sum<0)
            {
                sum=a[i];
                l=i;
                r=i;
            }
            else
            {
                sum+=a[i];
                r++;
            }
            if(sum>anss)
            {
                anss=sum;
                anssl=l;
                anssr=r;
            }
        }
        if(anss<0)
        {
            printf("0 %d %d\n",a[1],a[n]);
        }
        else
        {
            printf("%d %d %d\n",anss,a[anssl],a[anssr]);
        }
    }
}

 

 

Guess you like

Origin www.cnblogs.com/dyhaohaoxuexi/p/11432844.html