Max Sum(hd P1003)

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 an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).

Output

For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.

Sample Input

2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5

Sample Output

Case 1: 14 1 4 Case 2: 7 1 6

超时代码

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int a[100000],T,N,T1,j,i;
 5     scanf("%d",&T);
 6     T1=T;
 7     while(T--)
 8     {
 9         int Msum=0,sum=0,s=0,w=0;
10         printf("case %d:\n",T1-T);
11         scanf("%d",&N);
12         for(i=0;i<N;i++)
13             scanf("%d",&a[i]);
14         Msum=a[0];
15         for(j=0;j<N;j++)
16         {
17             for(i=j;i<N;i++)
18             {
19                 if(a[i]<=0)
20                 {
21                     sum+=a[i];
22                     continue;
23                 }
24                 sum+=a[i];
25                 if(Msum<sum)
26                 {
27                     s=j;
28                     w=i;
29                     Msum=sum;
30                 }
31             }
32             sum=0;
33         }
34         printf("%d %d %d\n\n",Msum,s+1,w+1);
35     }
36     return 0;
37 }

AC代码

 1 /*状态转移方程 d[i] = max(d[i-1]+a[i], a[i])
 2   d[i]表示以i位置结束的最大子序列之和。*/
 3 #include<stdio.h>
 4 int main()
 5 {
 6     int a[100000];
 7     int T,T1;
 8     scanf("%d",&T);
 9     T1=T;
10     while(T--)
11     {
12         int  sum=0,msum=0,i,x=0,y=0,start=0,end=0,N;
13         scanf("%d",&N);
14         for(i=0;i<N;i++)
15             scanf("%d",&a[i]);
16         sum=a[0];
17         msum=sum;
18         for(i=1;i<N;i++)
19         {
20             if(sum<0)/*dp[i-1]对a[i]不仅没有贡献,反而有损害,就应该舍弃*/
21             {
22                 x=y=i;
23                 sum=a[i];
24             }
25             else
26             {
27                 sum+=a[i];
28                 y=i;
29             }
30             if(sum>msum)
31             {
32                 msum=sum;
33                 start=x;
34                 end=y;
35             }
36         }
37         printf("Case %d:\n",T1-T);
38
39         if(T==0)
40         printf("%d %d %d\n",msum,start+1,end+1);
41         else
42         printf("%d %d %d\n\n",msum,start+1,end+1);
43
44     }
45 }
时间: 2024-11-02 18:51:05

Max Sum(hd P1003)的相关文章

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 cont

HDU 1003 Max Sum【动态规划求最大子序列和详解 】

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

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

[2016-03-28][HDU][1024][Max Sum Plus Plus]

时间:2016-03-28 17:45:33 星期一 题目编号:[2016-03-28][HDU][1024][Max Sum Plus Plus] 题目大意:从n个数字提取出一定数字组成m个部分,使得这个部分的总和最大 分析: dp[i][j]表示前i段计算第j个数字,dp[i][j] = max(dp[i - 1][j - 1] + a[j],dp[i][k] + a[j]); #include <algorithm> #include <cstring> #include &

HDU 1024 Max Sum Plus Plus

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21926    Accepted Submission(s): 7342 Problem Description Now I think you ha

Hdoj 1024 Max Sum Plus Plus 【DP】

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18861 Accepted Submission(s): 6205 Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b

hdoj 1003 Max Sum 【最大子段和】【贪心】

题意:... 策略:看着像贪心,感觉也是贪心. 很久之前做的,又做了一遍,好题. 代码: #include<stdio.h> #include<string.h> int s[100005]; int main() { int t, i, j, l, st, en, n, v = 1; scanf("%d", &t); while(t --){ scanf("%d", &n); for(i = 1; i <= n; i

杭电 1003 Max Sum

http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 142781    Accepted Submission(s): 33242 Problem Description Given a sequence a[1],a[2],a[3

HDU 3415 Max Sum of Max-K-sub-sequence 单调队列题解

本题又是一题单调队列题解. 技巧就是需要计算好前n项和Sn = a1 + a2 + ... an 这样方便处理. 记录一条单调队列,其意义是: q(head), q(head+1), ...q(tail) 其中头q(head)代表当前最佳解的起点 这样我们只需要在求某点为结尾的S[i] - S[q(head)就得到当前最佳值. 了解了单调数列,知道其中的记录意义,那么这道题就没有难度了.我也是了解这些信息之后就自己敲出代码的. 不过有些细节没写好也让我WA了几次. 最近少刷水题,而一直都是每天一