hdu1024 Max Sum Plus Plus

http://acm.hdu.edu.cn/showproblem.php?pid=1024

DP

最大m个不重叠连续子段和

 1 #include <stdio.h>
 2 
 3 #define N 1000100
 4 
 5 int a[N], dp1[N], dp2[N];
 6 
 7 int max(int x, int y)
 8 {
 9     return x>y? x: y;
10 }
11 
12 int main()
13 {
14     int i, j, n, m;
15     while(~scanf("%d%d", &m, &n))
16     {
17         for(i=1; i<=n; i++)
18         {
19             scanf("%d", a+i);
20             dp2[i] = 0;
21         }
22         dp2[0] = 0;
23         for(i=1; i<=m; i++)
24         {
25             dp1[i] = dp2[i-1] + a[i];
26             for(j=i+1; j<=n; j++)
27             {
28                 dp1[j] = max(dp2[j-1], dp1[j-1]) + a[j];
29             }
30             dp2[i] = dp1[i];
31             for(j=i+1; j<=n; j++)
32             {
33                 dp2[j] = max(dp2[j-1], dp1[j]);
34             }
35         }
36         printf("%d\n", dp2[n]);
37     }
38     return 0;
39 }

 

posted @ 2013-01-29 23:26  Yuan1991  阅读(132)  评论(0编辑  收藏  举报