Maximum continuous subsequence
Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 18461 accepted submission (s): 8202
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 0
# Include <iostream> # include <cstring> using namespace STD; # define m limit 5int A [m]; int main () {int N; while (~ Scanf ("% d", & N) {int ans, Max, I, X1, Y1, P, K = 0; scanf ("% d ", A + 1); if (a [1] <0) K ++; ans = max = A [1]; X1 = Y1 = p = 1; for (I = 2; I <= N; I ++) {scanf ("% d", A + I); if (a [I] <0) k ++; If (ANS <0) ans = A [I], P = I; elseans + = A [I]; If (max <ans) max = ans, x1 = P, Y1 = I;} If (k = N) printf ("% d \ n", 0, a [1], A [n]); elseprintf ("% d \ n", Max, a [X1], a [Y1]);} return 0 ;}