HDU-1003 Max sum dynamic planning

Source: Internet
Author: User

After reading the Dynamic Programming Method for this question, I suddenly felt that I had a better understanding.

Here we will explain the meaning of DP [I]. DP [I] indicates from 1 ~ I contains the largest substring of the number of I and, if the maximum sum of the number of I-1 containing the I-1 before, then the maximum substring containing the number of I is DP [I-1] + seq [I], otherwise DP [I] is equal to seq [I.

The dynamic recursion of this question does not directly give the final answer state, but it can be pushed to the answer according to another State. This is worth thinking about.

CodeAs follows:

# Include <cstring> # Include <Cstdlib> # Include <Cstdio> # Include <Algorithm> # Define Maxn 100005 Using   Namespace  STD;  Int N, DP [maxn], seq [maxn], p [maxn], begin, end; inline  Int Max ( Int X, Int  Y ){  Return X> Y? X: Y ;}  Void DP ( Int & Max) {memset (DP,  0 , Sizeof  (DP); DP [  1 ] = Seq [1  ]; Max = DP [ 1  ]; Begin = END = 1  ;  For ( Int I = 2 ; I <= N; ++ I) {DP [I] = Max (DP [I- 1 ] + SEQ [I], seq [I]);  If (DP [I- 1 ]> =0  ) {P [I] = P [I- 1  ];}  If (Max < DP [I]) {begin = P [I]; End = I; max = DP [I] ;}}  Int  Main (){  Int  T, ans; scanf (  " % D  " ,& T );  For ( Int CA = 1 ; Ca <= T; ++ CA) {scanf (  "  % D  " ,& N );  For ( Int I = 1 ; I <= N; ++I) {scanf (  "  % D  " ,& SEQ [I]); P [I] = I;} printf (  "  Case % d: \ n  "  , CA); DP (ANS); printf (  "  % D \ n  "  , ANS, begin, end );  If (Ca! =T) {puts (  ""  );}}  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.