Maximum continuous subsequence of 9 degrees 1011

Source: Internet
Author: User

Question 1011: maximum continuous subsequence time limit: 1 second memory limit: 32 MB special question: No submitted: 2456 solution: 1190 question Description: sequence of given K integers {N1, n2 ,..., NK}, which can be expressed as {Ni, Ni + 1 ,..., nj}, where 1 <= I <= j <= K. The maximum continuous subsequence is the element and the largest of all consecutive subsequences, such as the given sequence {-2, 11,-4, 13,-5,-2 }, its maximum continuous subsequence is {11,-4, 13}, and its maximum value is 20. Now, you need to output the first and last elements of the subsequence. Input: The test input contains several test cases. Each test case occupies two rows. Row 1st provides a positive integer K (K <10000), and row 2nd provides K integers separated by spaces. When K is 0, the input ends and the case is not processed. Output: For each test case, the first and last elements of the largest and maximum continuous subsequences are output 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 [cpp] # include <stdio. h> int main () {int k, I, a [10000], sum, tempsum, begin, end, position, count, zero; bool negative, first; while (scanf ("% d", & k), k) {sum = tempsum = begin = end = position = count = 0; negative = true; first = true; for (I = 0; I <k; I ++) scanf ("% d", & a [I]); for (I = 0; I <k; I ++) {tempsum + = a [I]; // write down the position where 0 appears for the first time. if (a [I] = 0 & first) {zero = I; first = false ;}; if (a [I]> = 0) negative = false; if (tempsum <0) {tempsum = 0; position = I + 1; // The sum of the current position and negative start from the next position} // cannot be equal to otherwise it cannot be guaranteed that it is a sequence with a small subscript if (tempsum> sum) {sum = tempsum; begin = position; end = I ;}} if (sum = 0) {if (negative) // all negative printf ("% d \ n", 0, a [0], a [k-1]); else printf ("% d \ n", 0, a [zero], a [zero]);} else printf ("% d \ n", sum, a [begin], a [end]);} return 0 ;} /*************************************** * *********************** Problem: 1011 User: windzhu Language: C ++ Result: Accepted Time: 20 MS Memory: 1012 kb ************************************** **************************/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.