HDU 1003 Max Sum && 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 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

最大连续子序列

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20109    Accepted Submission(s): 8884

Problem Description

给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., 
Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个, 
例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和 
为20。 
在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该 
子序列的第一个和最后一个元素。

Input

测试输入包含若干测试用例,每个测试用例占2行,第1行给出正整数K( < 10000 ),第2行给出K个整数,中间用空格分隔。当K为0时,输入结束,该用例不被处理。

Output

对每个测试用例,在1行里输出最大和、最大连续子序列的第一个和最后一个元 
素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。

Sample Input

6
-2 11 -4 13 -5 -2
10
-10 1 2 3 4 -5 -23 3 7 -21
6
5 -8 3 2 5 0
1
10
3
-1 -5 -2
3
-1 0 -2
0

Sample Output

20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0

Hint

Hint

Huge input, scanf is recommended.

两题的思路都差不多,假设现在只有s[0]一个元素,现要添加一个元素s[1],那么s[1]要么是新串的起点,要么是原串暂时的终点。如果之前的串的最大和小于0,那么s[1]的值加上原串之后只会小于s[1]本身,所以索性不加,s[1]自己新开一个串,自己作为起点。如果之前的串的最大和大于等于0,那么s[1]就增加到这个串上,并且暂时成为该串的终点。所以每加入一个元素,要么更新起点,要么更新暂时的终点。可以用一个数组DP[i]来保存以[i]为终点的子串的最大值,每次试图更新最大值即可。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define    MAX    100005
 5
 6 int    main(void)
 7 {
 8     int    t,n,count;
 9     int    dp[MAX];
10     int    max,start,START,end;
11     count = 0;
12
13     scanf("%d",&t);
14     while(t --)
15     {
16         count ++;
17
18         scanf("%d",&n);
19         for(int i = 0;i < n;i ++)
20             scanf("%d",&dp[i]);
21
22         max = dp[0];
23         start = START = end = 1;
24
25         for(int i = 1;i < n;i ++)
26         {
27             if(dp[i - 1] < 0 && dp[i - 1] != dp[i])    //讨论dp[i-1]小于0和大于等于0两种情况即可,后面的条件是为了符合题意
28                 start = i + 1;            //更新起点
29             else    if(dp[i - 1] >= 0)
30                 dp[i] = dp[i - 1] + dp[i];    //隐式地更新终点
31
32             if(max < dp[i])
33             {
34                 START = start;
35                 max = dp[i];
36                 end = i + 1;
37             }
38         }
39         printf("Case %d:\n",count);
40         printf("%d %d %d\n",max,START,end);
41         if(t)
42             puts("");
43     }
44
45     return    0;
46 }

max sum

上面的代码我用了两个循环,下面这个版本只用了一个,速度反而没第一个快,不知为何。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define    MAX    100005
 5
 6 int    main(void)
 7 {
 8     int    t,n,count;
 9     int    dp[MAX];
10     int    max,start,START,end;
11     count = 0;
12
13     scanf("%d",&t);
14     while(t --)
15     {
16         count ++;
17
18         scanf("%d",&n);
19         for(int i = 0;i < n;i ++)                //在读入的时候就顺便处理,不知为何会更慢
20         {
21             scanf("%d",&dp[i]);
22             if(!i)
23             {
24                 max = dp[0];
25                 start = START = end = 1;
26             }
27             else    if(dp[i - 1] < 0 && dp[i - 1] != dp[i])
28                 start = i + 1;
29             else    if(dp[i - 1] >= 0)
30                 dp[i] = dp[i - 1] + dp[i];
31
32             if(max < dp[i])
33             {
34                 START = start;
35                 max = dp[i];
36                 end = i + 1;
37             }
38         }
39
40         printf("Case %d:\n",count);
41         printf("%d %d %d\n",max,START,end);
42         if(t)
43             puts("");
44     }
45
46     return    0;
47 }

max sum_2

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define    MAX    10005
 5
 6 int    main(void)
 7 {
 8     int    k;
 9     int    dp[MAX],s[MAX],max,max_start,max_end,start;
10
11     while(scanf("%d",&k) && k)
12     {
13         for(int i = 0;i < k;i ++)
14             scanf("%d",&s[i]);
15
16         max = s[0];
17         dp[0] = s[0];
18         max_start = max_end = start = 0;
19
20         for(int i = 1;i < k;i ++)
21         {
22             if(dp[i - 1] < 0 && s[i] != dp[i - 1])        //一样的讨论是否为负就行了
23             {
24                 start = i;
25                 dp[i] = s[i];
26             }
27             else    if(dp[i - 1] >= 0)
28                 dp[i] = dp[i - 1] + s[i];
29
30             if(dp[i] > max)
31             {
32                 max = dp[i];
33                 max_start = start;
34                 max_end = i;
35             }
36         }
37
38         if(max < 0)
39         {
40             max = 0;
41             max_start = 0;
42             max_end = k - 1;
43         }
44         printf("%d %d %d\n",max,s[max_start],s[max_end]);
45     }
46
47     return    0;
48 }

最大连续子序列

这题还有下面这个版本,就是用个双重循环来选出起点和终点,然后就算这个区间的值,可以用一个循环算出以1为起点的值,然后再计算的时候就可以用这个数组推出来,感觉挺不错的,也有DP的思想在里面,虽然超时了。

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define    MAX    10005
 5
 6 int    main(void)
 7 {
 8     int        k,i,j;
 9     long    long    s[MAX],dp[MAX],box,max,max_i,max_j;
10
11     while(scanf("%d",&k) && k)
12     {
13         scanf("%lld",&dp[0]);
14         s[0] = dp[0];
15         for(int i = 1;i < k;i ++)
16         {
17             scanf("%lld",&dp[i]);
18             s[i] = dp[i];
19             dp[i] += dp[i - 1];                //DP[i]保存以1为起点i为终点的区间的值
20         }
21
22         max = dp[0];
23         max_i = max_j = 0;
24         for(int i = 0;i < k;i ++)
25             for(int j = i;j < k;j ++)
26             {
27                 if(i)
28                     box = dp[j] - dp[i - 1];    //i...j区间的值等于1...j的值减去1...i-1的值
29                 else
30                     box = dp[j];
31
32                 if(box > max)
33                 {
34                     max = box;
35                     max_i = i;
36                     max_j = j;
37                 }
38             }
39
40         if(max < 0)
41         {
42             max = 0;
43             max_i = 0;
44             max_j = k - 1;
45         }
46         printf("%lld %lld %lld\n",max,s[max_i],s[max_j]);
47     }
48
49     return    0;
50 }

最大连续子序列_2

时间: 2024-12-26 16:32:18

HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)的相关文章

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 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(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 (动规)

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 1231 最大连续子序列 DP题解

典型的DP题目,增加一个额外要求,输出子序列的开始和结尾的数值. 增加一个记录方法,nothing special. 记录最终ans的时候,同时记录开始和结尾下标: 更新当前最大值sum的时候,更新开始节点. const int MAX_N = 10001; long long arr[MAX_N]; int N, sta, end; long long getMaxSubs() { long long sum = 0, ans = LLONG_MIN; int ts = 0; for (int

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

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 156637    Accepted Submission(s): 36628 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【最大连续子序列和】【8月14】

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(最大子列和)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 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