码迷,mamicode.com
首页 > 其他好文 > 详细

最大子序列和 HDOJ 1003 Max Sum

时间:2015-08-10 19:58:08      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

 1 /*
 2     题意:求最大连续子序列和及两个端点
 3     累积遍历算法 O(n):依照sum<0将序列分块,最值在某一块上产生。dp也是同样的思路:dp[i] = max (dp[i-1] + a[i], a[i]) 其实是一样的
 4     1003就这么难??
 5     详细解释
 6 */
 7 /************************************************
 8  * Author        :Running_Time
 9  * Created Time  :2015-8-10 17:44:42
10  * File Name     :O(n).cpp
11  ************************************************/
12 
13 #include <cstdio>
14 #include <algorithm>
15 #include <iostream>
16 #include <sstream>
17 #include <cstring>
18 #include <cmath>
19 #include <string>
20 #include <vector>
21 #include <queue>
22 #include <deque>
23 #include <stack>
24 #include <list>
25 #include <map>
26 #include <set>
27 #include <bitset>
28 #include <cstdlib>
29 #include <ctime>
30 using namespace std;
31 
32 #define lson l, mid, rt << 1
33 #define rson mid + 1, r, rt << 1 | 1
34 typedef long long ll;
35 const int MAXN = 1e6 + 10;
36 const int INF = 0x3f3f3f3f;
37 const int MOD = 1e9 + 7;
38 int a[MAXN];
39 
40 int main(void)    {     //HDOJ 1003 Max Sum
41     int T, cas = 0;  scanf ("%d", &T);
42     while (T--) {
43         int n;  scanf ("%d", &n);
44         for (int i=1; i<=n; ++i)    {
45             scanf ("%d", &a[i]);
46         }
47         int sum = a[1], mx = a[1];
48         int l = 1, r = 1;
49         int ll = 1, rr = 1;
50         for (int i=2; i<=n; ++i)    {
51             if (sum <= 0)   {
52                 sum = a[i]; l = i;
53             }
54             else    sum += a[i];
55             if (mx < sum)   {
56                 mx = sum;   ll = l, rr = i;
57             }
58         }
59 
60         printf ("Case %d:\n%d %d %d\n", ++cas, mx, ll, rr);
61         if (T)  puts ("");
62     }
63 
64     return 0;
65 }
技术分享
 1 /************************************************
 2  * Author        :Running_Time
 3  * Created Time  :2015-8-10 18:24:00
 4  * File Name     :On_2.cpp
 5  ************************************************/
 6 
 7 #include <cstdio>
 8 #include <algorithm>
 9 #include <iostream>
10 #include <sstream>
11 #include <cstring>
12 #include <cmath>
13 #include <string>
14 #include <vector>
15 #include <queue>
16 #include <deque>
17 #include <stack>
18 #include <list>
19 #include <map>
20 #include <set>
21 #include <bitset>
22 #include <cstdlib>
23 #include <ctime>
24 using namespace std;
25 
26 #define lson l, mid, rt << 1
27 #define rson mid + 1, r, rt << 1 | 1
28 typedef long long ll;
29 const int MAXN = 1e6 + 10;
30 const int INF = 0x3f3f3f3f;
31 
32 int a[MAXN];
33 
34 int main(void)    {
35     int T, cas = 0;  scanf ("%d", &T);
36     while (T--) {
37         int n;  scanf ("%d", &n);
38         for (int i=1; i<=n; ++i)    {
39             scanf ("%d", &a[i]);
40         }
41         int sum = 0, mx = -INF, mn = 0;
42         int l = 0, ll = 0, rr = 1;
43         for (int i=1; i<=n; ++i)    {
44             sum += a[i];
45             if (sum - mn > mx)  {
46                 mx = sum - mn;
47                 ll = l; rr = i;
48             }
49             if (mn > sum)   {
50                 mn = sum;   l = i;
51             }
52         }
53         printf ("Case %d:\n%d %d %d\n", ++cas, mx, ll+1, rr);
54         if (T)  puts ("");
55     }
56 
57     return 0;
58 }
另一种做法

 

最大子序列和 HDOJ 1003 Max Sum

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4719011.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!