Max Sum (hdu-1003)

Source: Internet
Author: User

Max Sum (hdu-1003)

A very interesting dp is that it is interesting because its principle is actually very simple and the final answer can be obtained after a scan.

If the sum before the current number is less than 0, the largest sum starts from the current number. Otherwise, the largest sum is the sum before the current number.

So why is it true? We assume that the current number is continuous and less than 0, then his contribution to the following is negative, so no matter how large the number is, the maximum continuity and affirmation are not as good as the previous negative contribution.

Then, the maximum value of dp [I] is updated every time, which is equivalent to the end point of the continuous sum of enumeration.

 

#include
 
  #include
  
   #include
   
    #includeusing namespace std;int T,n,a[100005],dp[100005];int main(){    scanf("%d",&T);    int kase = 0;    while(T--){        scanf("%d",&n);        memset(dp,0,sizeof(dp));        for(int i=1;i<=n;i++) {            scanf("%d",&a[i]);        }        printf("Case %d:\n",++kase);        int ans = -1000000000,x1,x2,l,r;        dp[1] = a[1];        x1 = 1; ans = dp[1];        l=1; r=1;        for(int i=2;i<=n;i++){            if(dp[i-1]<0) {                x1 = i;                dp[i] = a[i];            } else dp[i] = dp[i-1] + a[i];            if(ans
    
     

 

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.