Nine degree OJ 1077 maximum sub-sequence and (dynamic planning)

Source: Internet
Author: User

Topic 1077: Maximum sequence and

time limit:1 seconds

Memory limit:32 MB

Special question: No

submitted:4836

Resolution:1401

Title Description:

Gives an integer sequence s, with n number, that defines the "sequence and" of T in one of the non-empty contiguous subsequence T.
For all non-empty contiguous subsequence t of S, the maximum sequence is obtained.
Variable conditions: N is a positive integer, n≤1000000, the result sequence and within the range ( -2^63,2^63-1).

Input:

The first behavior is a positive integer n, and the second behaves n integers, representing the number in the sequence.

Output:

Input may include multiple sets of data, for each set of input data,
Only one number is output, representing the maximum sequence and.

Sample input:
51 5-3 2 461-2 3 4-10 64-3-1-2-5
Sample output:
97-1
 #include < stdio.h> #include <string.h> #include <math.h>long long int ms;int n;long long int a[1000010];long long int b       [1000010];int i;int J;int k;void so () {b[0]=a[0];           for (int i = 1; i < N; i++) {if (B[i-1]+a[i]>a[i]) b[i]=b[i-1]+a[i];           else B[i]=a[i];       if (b[i]>ms) ms=b[i];    }}int Main (int argc, char *argv[]) {//Freopen ("1077.in", "R", stdin);        while (~SCANF ("%d", &n)) {memset (a,0,sizeof (a));        for (I=0;i<n;++i) scanf ("%lld", &a[i]);        MS=A[0];        So ();    printf ("%lld\n", MS); } return 0;} /************************************************************** problem:1077 User:kirchhoff language:c Resu lt:accepted time:250 Ms memory:16540 kb****************************************************************/


Set B[J] denotes the maximum and of the subsequence at the end of section J, with A[j]. Note: B[j] is not the sum of the largest contiguous subsequence in the number of previous j-1, but only the sum of the largest contiguous subsequence containing a[j]. We find the maximum value in B[j], which is the number of the maximal contiguous subsequence we are seeking. Recursive formula: B[j]=max{a[j],b[j-1]+a[j]}


Nine degree OJ 1077 maximum sub-sequence and (dynamic planning)

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.