(DP)HDU - 1003 Max Sum

This is a DP entry title, knowledge is the "largest contiguous subsequence"


Title effect: to give you a number sequence of length n, whichever is a contiguous sequence in which, and the maximum requirements;


Analysis: This question is a naked, no bells and whistles, mainly write state transition equation DP [I] DP = max {[-I. 1] + A [I], A [I]};    DP [I] i is the optimal solution for the location of the end position.
        

   For A [i] i on the location, some of dp [i] contributed.

   I for the previous position, their optimal solution dp [i-1], when [i-1]> when dp = 0, dp [i-1] of the DP [i] contributed; conversely, DP [i-1] have a negative effect on dp [i];


My mistake: boundary dp [1] forget to do a deal with
      2. To start of each update, iron Han Han I direct result of last updated when the results. .

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 const int Maxn = 100000 + 5;
 6 int T,n;
 7 int dp[Maxn];
 8 int main()
 9 {
10     scanf("%d",&T);
11     for(int t=1;t<=T;t++)
12     {
13         scanf("%d",&n);
14         for(int i=1;i<=n;i++)
15             scanf("%d",&dp[i]);
16         int ans = dp[1];//用边界对ans初始化
17         int start = 1,ends = 1,fstart=1;
18         for(int i=2;i<=n;i++)
19         {
20             if(dp[i-1]>=0 )
 21 is                  DP [I] + DP = [I- . 1 ];
 22 is              the else 
23 is                  Start = I; // Update the starting point is not necessarily the optimal solution 
24              IF (DP [I]> ANS)
 25              {
 26 is                  ANS = DP [I];
 27                  FSTART = Start; // update when updating the optimal solution starting 
28                  ends = I;
 29              }
 30          }
 31 is          IF (! = T . 1 ) the puts ( "" );
 32          the printf ( "Case %d:\n",t);
33         printf("%d %d %d\n",ans,fstart,ends);
34     }
35     return 0;
36 }
View Code

 

Guess you like

Origin www.cnblogs.com/chen-tian-yuan/p/11221296.html