Max Sum(最大子串和)

Max Sum

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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


Analysis

  状态转移方程 dp[i] = max( dp[i-1] + a[i] , a[i] )

 1 #include <stdio.h>
 2 #include <string.h>
 3
 4 int a[100002];
 5
 6 main()
 7 {
 8     int T,N,kkk=1,i,j;
 9
10     scanf("%d",&T);
11     while(T--)
12     {
13         int max,max_s,max_e,sum,sum_s;
14         //memset
15         printf("Case %d:\n",kkk++);
16
17         scanf("%d",&N);
18         for(i=1;i<=N;i++)
19         {
20             scanf("%d",&a[i]);
21         }
22
23         max = sum = a[1];
24         max_s = max_e = sum_s = 1;
25
26         //printf("sum = %d  max = %d\n",sum,max);
27         for(i=2;i<=N;i++)
28         {
29             if(sum + a[i] < a[i])
30             {
31                 sum = a[i] ;
32                 sum_s = i ;
33             }
34             else sum = sum + a[i] ;
35
36             if(sum > max)
37             {
38                 max = sum ;
39                 max_s = sum_s;
40                 max_e = i;
41             }
42             //printf("sum = %d  max = %d\n",sum,max);
43         }
44         printf("%d %d %d\n",max,max_s,max_e);
45         if(T)
46         printf("\n");
47     }
48 }
时间: 2024-11-03 00:57:49

Max Sum(最大子串和)的相关文章

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

Max Sum (hdu 1003 简单DP水过)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 161294    Accepted Submission(s): 37775 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; 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

1003 Max Sum(动态规划)

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

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