dp/hdu 1003 Max Sum

题目

  给出一列数,求出最大的子段和,并输出起始点和结束点

分析

  设d[i]为末尾数是i的最大和,我们可以得到

  d[i-1]>=0时,d[i]=d[i-1]+a[i];

  d[i-1]<=0时,d[i]=a[i];

  i从1到n都算过一遍后,找出最大的d[i],则此时的i为结束点。再从结束点倒着向前加,直到找到与答案相同的位置,记为起始点

Accepted Code

 1 /*
 2     PROBLEM:hdu1003
 3     AUTHER:NicoleLam
 4     MEMO:dp
 5 */
 6 #include<cstdio>
 7 const int MAXN=100010;
 8
 9 int a[MAXN],d[MAXN];
10 int main()
11 {
12     int tot;
13     scanf("%d",&tot);
14     for (int t=1;t<=tot;t++)
15     {
16         int n;
17         scanf("%d",&n);
18         for (int i=0;i<MAXN;i++){a[i]=0;d[i]=0;}
19         for (int i=1;i<=n;i++) scanf("%d",&a[i]);
20         d[1]=a[1];
21         for (int i=2;i<=n;i++)
22         {
23             if (d[i-1]>=0) d[i]=d[i-1]+a[i];
24             else d[i]=a[i];
25         }
26         int ans=d[1],ed=1;
27         for (int i=2;i<=n;i++)
28             if (ans<d[i])
29             {
30                 ans=d[i];
31                 ed=i;
32             }
33         int temp=0,st=ed;
34         for (int i=ed;i>0;i--)
35         {
36             temp+=a[i];
37             if (temp==ans) st=i;
38         }
39         printf("Case %d:\n",t);
40         printf("%d %d %d\n",ans,st,ed);
41         if (t!=tot) printf("\n");
42     }
43     return 0;
44 }
时间: 2024-10-06 19:55:23

dp/hdu 1003 Max Sum的相关文章

HDU 1003 Max Sum(dp,最大连续子序列和)

Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input

HDU 1003 Max Sum 最大连续子序列的和

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains

HDU 1003 Max Sum (动规)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 141547    Accepted Submission(s): 32929 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

[ACM] hdu 1003 Max Sum(最大子段和模型)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 135262    Accepted Submission(s): 31311 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

HDU - 1003 - Max Sum &amp;&amp; POJ - 1050 - To the Max (经典DP问题)

题目传送:HDU - 1003 思路:最大子序列和 dp[i]= a[i]   (dp[i-1]<0) dp[i]= dp[i-1]+a[i]   (dp[i-1]>=0) AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include

HDU 1003 Max Sum(DP)

题目地址:HDU 1003 DP好弱..先补补DP的基础... 这题就是记录起始点与终止点,然后每当发现一个更大的,就更新.从左往右扫一遍就行了. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #

HDU 1003 Max Sum &amp;&amp; HDU 1231 最大连续子序列 (DP)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 154155    Accepted Submission(s): 35958 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max su

hdu 1003 Max Sum(dp)

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains

HDU 1003 Max Sum (dp)

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains