Maximum continuous subsequence and
Maximum continuous subsequenceTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 19156 Accepted Submission (s): 8532
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 #include
#include #include
#define MAXN 10000 + 10using namespace std;int num[MAXN];int main(){ int n; int MAX,left,right; int flag; while(~scanf("%d",&n)) { memset(num,0,sizeof(num)); if(n == 0) { break; } int i; for(i=0; i
MAX) { right = num[i]; MAX = count; left = flag; } } if(MAX >= 0) { printf("%d %d %d\n",MAX,left,right); } else { printf("0 %d %d\n",num[0],num[n-1]); } } return 0;}