Sdut 1351 Max Sum

Source: Internet
Author: User

Max Sum Time limit:2000ms Memory limit:32768k have questions? Dot here ^_^ Title description Given 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 = 14. Enter the first line of the input cont Ains 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 1000). Output for each test case, you should output both 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. Example input
25 6-1 5 4-77 0 6-1 1-6 7-5
Sample output
Case 1:14 1 4Case 2:7 1 6
Tip hdoj1003 Note: The background test data volume is large, please use the C language scanf input. Avoid timeouts!!!! SOURCE IGNATIUS.L



#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <    Stdlib.h> #include <queue>using namespace Std;int a[100100],b[100100];int main () {int T;    int k = 0;    scanf ("%d", &t);        while (t--) {int n;        scanf ("%d", &n);        for (int i=0;i<n;i++) {scanf ("%d", &a[i]);        } B[0] = a[0];            for (int i=1;i<n;i++) {if (b[i-1]<0) {b[i] = A[i];            } else {B[i] = A[i] + b[i-1];        }} int xx,yy;        int maxx =-999999;                for (int i=0;i<n;i++) {if (Maxx<b[i]) {Maxx = B[i];            yy = i+1;        }} int pmax = 0;            for (int i=yy-1;i>=0;i--) {Pmax + = A[i];            printf ("Pmax =%d Maxx =%d\n", Pmax,maxx);        if (Pmax = = Maxx)    {xx = i+1;            Break        }} printf ("Case%d:\n", ++k);        printf ("%d%d%d\n", maxx,xx,yy);        if (T) {printf ("\ n");  }} return 0;}


Sdut 1351 Max Sum

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.