HDU1024Max Sum Plus Plus(M段最大和)

题意:求一个数组中 M 段的 最大和

没看明白怎么搞得

抽空来看,写的不赖

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdio>
 5 using namespace std;
 6 const int INF = 0x3f3f3f3f;
 7 const int N = 1000000 + 10;
 8 int dp[N], num[N], pre[N];
 9 int main()
10 {
11     int m, n;
12     while (scanf("%d%d", &m, &n) != EOF)
13     {
14         for (int i = 1; i <= n; i++)
15         {
16             scanf("%d", &num[i]);
17         }
18         memset(pre, 0, sizeof(pre));
19         memset(dp, 0, sizeof(dp));
20         int ans = -INF, maxn;
21         for (int i = 1; i <= m; i++)
22         {
23             maxn = -INF;
24             for (int j = i; j <= n; j++)
25             {
26                 dp[j] = max(dp[j - 1], pre[j - 1]) + num[j];
27                 pre[j - 1] = maxn;
28                 maxn = max(maxn, dp[j]);
29             }
30             //if (ans < maxn)
31               //  ans = maxn;
32         }
33         printf("%d\n", maxn);
34     }
35 
36     return 0;
37 }
View Code

 

posted @ 2016-03-31 21:30  zhaop  阅读(140)  评论(0编辑  收藏  举报