Hangzhou Electric 1003

Source: Internet
Author: User

Topic Link Click Open link

problem Descriptiongiven A sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + ( -1) + 5 + 4 =.

Inputthe first line of the input contains an integer T (1<=t<=20) which means the number of test cases. Then T lines follow, each line starts with a number N (1<=n<=100000), then n integers followed (all the integers is b etween-1000 and).

outputfor Each test case, you should output of the lines. The first line was "Case #:", # means the number of the the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end posi tion of the sub-sequence. If there is more than one result, output the first one. Output a blank line between cases.

Sample Input2
5 6-1 5 4-7
7 0 6-1 1-6 7-5
Sample outputcase 1:
1 4

Case 2:
7 1 6

1: The input data may be negative;
2: The output format should be noted.

#include <stdio.h> int a[100010]; int main (void) {<span style= "white-space:pre" ></span>int n,m,k = 1,i,pos,start,end;<span style= " White-space:pre "></span>scanf ("%d ", &n), <span style=" White-space:pre "></span>while (n--) <span style= "White-space:pre" ></span>{<span style= "White-space:pre" ></span>scanf ("%d", &AMP;M); <span style= "White-space:pre" ></span>for (i = 1; I <= m; i++) <span style= "White-space:pre" ></span>{<span style= "White-space:pre" ></span>scanf ("%d", &a[i]); <span style= " White-space:pre "></span>}<span style=" White-space:pre "></span>int s = 0,max = A[1];<span Style= "White-space:pre" ></span>pos = start = end = 1;<span style= "White-space:pre" ></span>for (i = 1; I <= m; i++) <span style= "White-space:pre" ></span>{<span style= "White-space:pre" ></span>if (S < 0 ) <span style= "White-space:pre" ></span> {<span style= "white-space:pre" ></span>s = A[i];<span style= "white-space:pre" ></span>pos = i; <span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span>else<span Style= "White-space:pre" ></span>s + = A[i];<span style= "White-space:pre" ></span>if (Max < s) <span style= "White-space:pre" ></span>{<span style= "white-space:pre" ></span>max = s;< Span style= "white-space:pre" ></span>start = Pos;<span style= "white-space:pre" ></span>end = i; <span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span>}<span Style= "White-space:pre" ></span>printf ("Case%d:\n%d%d%d\n", k++,max,start,end); <span style= " White-space:pre "></span>if (n > 0) <span style=" White-space:pre "></span>printf (" \ n ");< Span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></span>return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hangzhou Electric 1003

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.