Hdu1231 maximum continuous subsequence (Dynamic Planning)

Source: Internet
Author: User
Maximum continuous subsequence

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 39872 accepted submission (s): 18076


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 Input6-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 output20 11 1310 1 410 3 510 10 100-1-20 0 0 HintHinthuge input, scanf is recommended.

Question: Give an integer sequence, find a continuous and maximum subsequence, output sum, the first element of the subsequence, And the last element of the subsequence.

Problem: state transition equation: DP [I] = max (A [I], DP [I-1] + A [I])

Emm is clearly a dynamic plan, and it always feels violent.

 

 1 #include<bits/stdc++.h> 2 using namespace std; 3 int a[10005],dp[10005]; 4 int main() 5 { 6     int k; 7     while(~scanf("%d",&k),k) 8     { 9         memset(a,0,sizeof(a));10         memset(dp,0,sizeof(dp));11         for(int i=1;i<=k;i++)12         {13             scanf("%d",&a[i]);14         }15         int maxx=-9999,s,e=-9999;16         for(int i=1;i<=k;i++)17         {18             dp[i]=max(a[i],dp[i-1]+a[i]);19             if(maxx<dp[i])20             {21                 maxx=dp[i]; 22             }    23         }24         for(int i=1;i<=k;i++)25         {26             if(dp[i]==maxx&&e==-9999)27             {28                 e=i;break;29             }30         }31         for(int i=e;i>=1;i--)32         {33             if(dp[i]==a[i])34             {35                 s=i;break;36             }37         }38         if(maxx<0)39         {40             printf("0 %d %d\n",a[1],a[k]);41         }else 42         {43             printf("%d %d %d\n",maxx,a[s],a[e]);44         }45     }46     return 0;47 }

 

Hdu1231 maximum continuous subsequence (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.