Maximum continuous subsequence
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 18316 accepted submission (s): 8127
The Problem description is a sequence of {N1, N2,..., nk} Given K integers. Any continuous subsequence can be represented as {Ni, Ni + 1 ,...,
NJ}, where 1 <= I <= j <= K. The maximum continuous subsequence is the element and the largest of all consecutive subsequences,
For example, for a given sequence {-2, 11,-4, 13,-5,-2}, its maximum continuous subsequence is {11,-4, 13}, the largest and
Is 20.
In this year's data structure examination paper, the maximum sum of programming requirements is required. Now, a requirement is added, that is,
The first and last elements of the subsequence.
The input test input contains several test cases. Each test case occupies two rows, Row 1 provides a positive integer k (<1st), and row 3 provides K integers, separated by spaces. When K is 0, the input ends and the case is not processed.
For each test case, output the first and last elements of the largest sum and maximum continuous subsequences in one row.
, Separated by spaces. If the maximum continuous subsequence is not unique, the smallest sequence numbers I and j are output (for example, 2nd and 3 groups in the input sample ). If all k elements are negative, the maximum value is 0, and the first and last elements of the entire sequence are output.
Sample Input
6-2 11 -4 13 -5 -210-10 1 2 3 4 -5 -23 3 7 -2165 -8 3 2 5 01103-1 -5 -23-1 0 -20
Sample output
20 11 1310 1 410 3 510 10 100 -1 -20 0 0HintHint Huge input, scanf is recommended.
Source Zhejiang University Computer postgraduate review computer examination-2005
Recommendjgshining | we have carefully selected several similar problems for you: 1176 1257 1069 1159
Water... Hangzhou power is not good. Wa is a small array, not a stack explosion...
# Include <iostream> # include <string. h>Using namespaceSTD;# Define M 11000IntA[M],DP[M]; Int main (intI, IntJ, IntK) {IntN,First=0,Last=0,Max=0; While (CIN>N) {If (N=0) Break;First=0,Last=1;Memset(A,0, Sizeof (A));Memset(DP,0, Sizeof (DP); (I=1;I<=N;I++ ){Scanf("% D",&A[I]);DP[I] =A[I];} // The Dp value is assigned here. So the following do not let DP [I-1] = 0, should be DP [I] must be greater than DP [I-1]. (I=1;I<=N;I++) If (A[I]> =0)First=1;// Determine whether all values are negative. Max=A[1]; // My array starts from 1. For (I=1;I<=N;I++) {If (DP[I] <DP[I-1] +A[I])DP[I] =DP[I-1] +A[I]; If (DP[I]>Max){Max=DP[I];Last=I;}} (I=Last;I> =0;I--) // If the value is greater than 0, it is a positive excitation, plus DP [I-1] is bigger than the present. If (DP[I-1] <=0) Break; If (First)Printf("% D \ n",Max,A[I],A[Last]); Max is the largest (actually redundant ). ElsePrintf("0% d % d \ n",A[1],A[N]); // If all values are negative, the first and last 0 values are output. } Return0;}
HDU 1231 maximum continuous subsequence (dynamic)