HDU 1231: maximum continuous subsequence (DP)

Source: Internet
Author: User

Maximum continuous subsequence Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 18461 accepted submission (s): 8202


The Problem description is a sequence of {N1, N2,..., nk} Given K integers. Any 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, the maximum sum of programming requirements is required. Now, a requirement is added, that is,
The first and last elements of the subsequence.
 
The input test input contains several test cases. Each test case occupies two rows, Row 1 provides a positive integer k (<1st), and row 3 provides K integers, separated by spaces. When K is 0, the input ends and the case is not processed.
 
For each test case, output the first and last elements 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 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 0HintHint Huge input, scanf is recommended.


The same is true for hdu1003 Max sum .. Water ..


#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<stdlib.h>#include<vector>#include<queue>#include<cmath>using namespace std;const int maxn = 100000 + 50;int n;int a[maxn];int start;int last;int temp;int ans;int sum;int t;int main(){    while(scanf("%d", &n)==1&&n)    {        t = 0;        memset(a, 0, sizeof(a));        for(int i=1; i<=n; i++)        {            scanf("%d", &a[i]);            if(a[i]<0)            t++;        }        if( t==n ){printf("0 %d %d\n", a[1], a[n]);continue;}        start = 1;        last = 1;        temp = 1;        sum = a[1];        ans = a[1];        for(int i=2; i<=n; i++)        {            if( sum<0 )            {                temp = i;                sum = 0;            }            sum += a[i];            if( sum>ans )            {                ans = sum;                start = temp;                last = i;            }        }        printf("%d %d %d\n", ans, a[start], a[last]);    }    return 0;}




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.