Maximum continuous subsequence of ACM-DP -- hdu1231

Source: Internet
Author: User
* *********************************** Please specify the source: bytes ***************************************
Maximum continuous subsequence Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 17941 accepted submission (s): 7941

Problem description specifies the sequence of K integers {N1, N2,..., nk}. The random 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, encoding and coding are required to obtain the largest sum. Now, a requirement must be added to output
The first and last elements of the subsequence.
 
The input partition trial input includes several partition trial examples. Each partition sample occupies two rows. The first row contains a positive integer k (<1st) and the second row contains k integers separated by spaces. When K is 0, the input ends and the case is not processed.
 
Output for each shard trial example, output the first and last element 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 example ). 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 Huge input, scanf is recommended.
Question: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1231
Continue to do some DP questions. This is the maximum continuous subsequence. This state transfer equation is very easy, that is, DP [I] = max (DP [I-1] + A [I], a [I]) due to the need to output the first and last positions, so I created an array to store and reach the header of the current position.
In this case, if all the data is negative, the sum is 0, and the first and last positions of the entire array are output. In this case, a bool variable can be used to input data, one by one inference-62ms can also create an array, and then sort it by sort to determine whether the maximum number is negative-125 Ms, and there is an additional 10000 space consumption
/*************************************** **************************************** * ** Author: tree ** from: http://blog.csdn.net/lttree ** title: maximum continuous subsequence ** Source: HDU 1231 ** hint: DP *************************************** **************************************** * **/# include <stdio. h> int A [10001], sum [10001], pre [10001]; int main () {int N, I; int Max, max_ I; // isnegtive to determine whether all numbers are smaller than 0 bool isnegtive; while (Scanf ("% d", & N )! = EOF & N) {isnegtive = false; for (I = 0; I <n; ++ I) {scanf ("% d", & A [I]); if (A [I]> = 0) isnegtive = true;} // assume that all the numbers are smaller than 0, and you do not need to calculate them later. Directly output if (! Isnegtive) {printf ("0% d % d \ n", a [0], a [n-1]); continue ;} // calculate the maximum sequence and sum [0] = pre [0] = A [0]; for (I = 1; I <n; ++ I) {If (sum [I-1] + A [I]> A [I]) {sum [I] = sum [I-1] + A [I]; pre [I] = pre [I-1];} else sum [I] = pre [I] = A [I];} // find the largest subsequence and, save the subscript max =-999999; for (I = 0; I <n; ++ I) {If (sum [I]> MAX) {max = sum [I]; max_ I = I ;}} printf ("% d \ n", Max, pre [max_ I], a [max_ I]);} return 0 ;}



Maximum continuous subsequence of ACM-DP -- hdu1231

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.