MAX SUM (HDU_1003--DP)

Source: Internet
Author: User

Topic:

?

Test Instructions: The maximum value of the number of successive subsequence sequences in the given sequence, and the starting and ending points of this subsequence.

idea:DP to find the maximum value of the current position,dp[i] = max (dp[i-1], a[i]); The position of the maximum is the end point, since you know the sum of the size, then, just find the sum of that interval is the value can be, here you can use a prefix and array to find the starting position.

PS:DP still have to think more, practise more!!

Code:

1#include <iostream>2#include <queue>3#include <cstdio>4#include <algorithm>5#include <cmath>6#include <cstring>7#include <queue>8#include <map>9#include <vector>Ten #defineINF 0x3f3f3f3f One #defineFRE () freopen ("In.txt", "R", stdin) A  - using namespacestd; -typedefLong Longll; the Const intMAXN = 1e5+Ten; - intA[MAXN],DP[MAXN],SUM[MAXN]; - intMain () - { +     intt,n,cnt =1; -scanf"%d",&T); +      while(t--) A     { atMemset (A,0,sizeof(a)); -Memset (DP,0,sizeof(DP)); -memset (SUM,0,sizeof(sum)); -scanf"%d",&n); -          for(inti =1; i<=n; i++) -         { inscanf"%d",&a[i]); -Sum[i] = sum[i-1]+A[i]; to         } +         intSt =0, en =0, Mmax =-inf,f =1; -          for(inti =1; i<=n; i++) the         { *Dp[i] = max (dp[i-1]+a[i],a[i]); $             if(dp[i]>Mmax)Panax Notoginseng             { -Mmax =Dp[i]; theEn =i; +             } A         } the          for(inti =1; i<=en; i++) +         { -             if(sum[en]-sum[i-1] ==Mmax) $             { $St =i; -                  Break; -             } the         } -         if(cnt!=1) printf ("\ n");Wuyiprintf"Case %d:\n", cnt++); theprintf"%d%d%d\n", mmax,st,en); -     } Wu     return 0; - } About  $ /* - Sample Input: - 2 - 5 6-1 5 4-7 A 7 0 6-1 1-6 7-5 + Sample output: the Case 1: - 1 4 $  the Case 2: the 7 1 6 the */
View Code

MAX SUM (HDU_1003--DP)

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.