Data structure (Chen) job title first week

Source: Internet
Author: User

1-1 Maximum child column and problem 20ptstime limit10000 MSMemory Limit65536 KBCode Length Limitations8000 BProcedures for the award of questions Standard

Given a sequence of K integers {N1, N2, ..., NK}, "Continuous child column" is defined as {Ni, ni+1, ..., Nj}, where 1 <= i <= J <= K. "Maximum child columns and" are defined as the and the largest of all contiguous child column elements. For example, given sequence {-2, 11,-4, 13,-5,-2}, its contiguous sub-columns {11,-4, 13} have the largest and 20. You are now asked to write a program that calculates the maximum sub-columns of a given integer sequence.

Input format:

Enter line 1th to give the positive integer k (<= 100000), and the 2nd line to give the k integers, separated by a space.

Output format:

Outputs the maximum child columns in a row. If all integers in the sequence are negative, the output is 0.

Input Sample:
6-2 11-4 13-5-2
Sample output:
20


The problem in the grandmother's video, there are 4 ways, the best is the online algorithm, here I no longer repeat, directly affixed to the code, the details please see the grandmother's video explanation.
1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6     intN;7CIN >>N;8 9     intthis_sum=0;Ten     intmax_sum=0; One     intElement=0; A  -      for(intI=0; i<n;i++) -     { theCIN >>element; -This_sum + =element; -         if(This_sum >max_sum) -Max_sum =this_sum; +         if(This_sum <0) -This_sum =0; +     } A  at     if(max_sum<0) -cout <<0; -     Else -cout <<max_sum; -  -     return 0; in}

1-2. Maximum subsequence sum time limit of MS memory limit 65536 KB code length limit 8000 B procedure StandardAuthor Chen, Yue

Given a sequence of K integers {N1, N2, ..., NK}. A continuous subsequence is defined to be {Ni, ni+1, ..., Nj} where 1 <= i <= J <= K. The Maximum subsequence is the continuous subsequence which have the largest sum of its elements. For example, given sequence {-2, one, -4, -5, 2}, its maximum subsequence are {One, -4, all} with the largest sum bei ng 20.

Now is supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains the one test case. Each case occupies the lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output on one line the largest sum, together with the first and the last numbers of the maximum Subseq Uence. The numbers must is separated by one space, but there must is no extra space at the end of a line. In case the maximum subsequence are not unique, output the one with the smallest indices I and j (as shown by the Samp Le case). If all the K numbers was negative, then it maximum sum is defined to being 0, and you were supposed to output the first and T He last numbers of the whole sequence.

Sample Input:
10-10 1 2 3 4-5-23 3 7-21
Sample Output:
10 1 4

This problem is similar to the previous one, except for a requirement that the first and last elements of the output maximum child columns. My approach is to use two variables, l_temp and r_temp, to record the first and last elements of the current child column, and if the child column is larger than the current record, then update the top and end elements L_max and R_max of the largest child column with these two variables. If the current child column is less than 0, the recalculation starts from the next element. But there are a few special points to note:

1. If the input element is all negative, the maximum sum is 0, and the negative number at the end of the output.
2. If the input elements are all negative and 0, the maximum and 0, but the output should be 0 0 0.
For example, input-3 0-6, output is 0 0 0, not 0-3-6
3. The end-to-end elements of the output should have a minimum subscript, such as:
Input 3 7-10 11, you should output 11 3 11, instead of 11 11 11

1#include <iostream>2 using namespacestd;3 4 intMain ()5 {6     intN;7CIN >>N;8 9     intthis_sum=0;Ten     intmax_sum=0; One     intl_max=0; A     intr_max=0; -     intl_temp=0; -     intr_temp=0; the     BOOLbegin_again=false;//determine if this_sum starts again -     BOOLAllnegative=true;//determines whether the array element is all 0 -  -     int*element =New int[N]; +      for(intI=0; i<n;i++) -     {         +CIN >>Element[i]; A     } at  -l_temp=element[0]; -r_temp=element[0]; -      for(intI=0; i<n;i++) -     {         -This_sum + =Element[i]; in  -         if(element[i]>=0) toAllnegative=false; +  -         if(Begin_again) the         { *l_temp=Element[i]; $r_temp=Element[i];Panax Notoginsengbegin_again=false; -         } the         Else +         { Ar_temp=Element[i]; the         } +  -         if(This_sum >max_sum) $         { $Max_sum =this_sum; -L_max =l_temp; -r_max=r_temp; the         } -         ElseWuyi             if(This_sum <0)//can only use <0, can not <=0, because to output smallest indices, if 3 7-10 11, you should output 11 3 11, instead of one by one the             { -This_sum =0; Wubegin_again=true;//Start Again -             } About     } $  -     if(max_sum==0) -         if(allnegative) -cout <<0<<' '<< element[0] <<' '<< element[n-1] <<Endl; A         Else +cout <<0<<' '<<0<<' '<<0<<Endl; the     Else -cout << max_sum <<' '<< L_max <<' '<< R_max <<Endl; $  the     return 0; the}

Data structure (Chen) job title first week

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.