Maximum continuous subsequenceTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total Submission (s): 21205 Accepted Submission (s): 9417
problem Descriptiongiven the sequence of k integers {N1, N2, ..., NK}, any contiguous subsequence can be expressed as {Ni, ni+1, ...,
Nj}, where 1 <= i <= J <= K. The maximal contiguous subsequence is the element and the largest of all successive subsequence sequences,
For example, given sequence {-2, 11,-4, 13,-5,-2}, its maximum contiguous subsequence is {11,-4, 13}, Max
to 20.
In this year's data structure exam, it is required to write the program to get maximum and, now add a requirement, that also need to output the
The first and last elements of a subsequence.
InputThe test input contains several test cases, each of which occupies 2 rows, the 1th line gives the positive integer K (< 10000), the 2nd line gives K integers, and the middle is separated by a space. When k is 0 o'clock, the input ends and the use case is not processed.
Outputfor each test case, the first and last elements of the largest and longest contiguous subsequence are output in 1 rows
The middle is separated by a space. If the maximum consecutive subsequence is not unique, the output sequence number I and J is the smallest one (as in the 2nd, 3 groups of the input sample). If all k elements are negative, define their maximum and 0, outputting the entire sequence of the first and the end elements.
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
1310 1 410 3 510 100-1 -20 0 0 Hint Hint Huge input, scanf is recommended.
Thinking of solving problems
This problem clearly uses the knowledge of dynamic programming.
Need to use temporary and value, temporary start value, sum value, start value, end value
The temporary start value is used because -10 1 2 3 4-5-23 3 7-21 The number of preceding and 0 in a sequence may occur, but this value is not 0
The real start value is determined by the temporary and the value.
Determine the temporary start value: front and less than equals 0 && this value is greater than 0
Judge temporary and value: Add this value if it is greater than 0, otherwise change to 0
If the temporary and value is greater than the sum value, then the sum value, the start value, and the ending value of three elements are changed.
Code
#include <iostream>using namespace Std;int num[11000];int main () {int n;int i,j,k;int ok;int now,nsta;int sta,end, Sum;while (cin>>n,n) {ok=0;//judgment is not negative for (i=0;i<n;i++) { cin>>num[i]; if (num[i]>=0) ok=1;} if (ok==0) cout<<0<< "" <<num[0]<< "" <<num[n-1]<<endl;else{ now=sum=0; nsta=sta=end=0; Initialize all, otherwise affect the next operation for (i=0;i<n;i++) { if (now<=0&&num[i]>0) nsta=num[i]; Determine the temporary start value: front and less than or equal to zero && this value is greater than 0 if (now+num[i]>0) now+=num[i]; Just add this number more than 0 to keep adding, otherwise become 0 else now=0; if (now>sum) { //if temporary and greater than sum, change sum, start value, end value sum=now; Sta=nsta; To determine the starting value according to the provisional and End=num[i];} } cout<<sum<< "<<sta<<" "<<end<<endl;}} return 0;}
1504061900-hd-maximum consecutive subsequence