HDU1024 Max Sum Plus Plus(dp)

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

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 #include<cmath>
 5 #include<cstring>
 6 #include<queue>
 7 #include<cstdio>
 8 #include<unordered_map>
 9 #define inf 0x3f3f3f3f
10 using namespace std;
11 typedef long long ll;
12 const int maxn = 1e6+15;
13 int dp[maxn],Max[maxn],a[maxn];
14 int m,n; 
15 int main(){
16     while(scanf("%d%d",&m,&n)!=EOF){
17         for(int i = 1;i<=n;i++) scanf("%d",&a[i]);
18         memset(dp,0,sizeof(dp));
19         memset(Max,0,sizeof(Max));
20         int temp = 0;
21         for(int i = 1;i<=m;i++){
22             temp = -inf;
23             for(int j = i;j<=n;j++){
24                 dp[j] = max(dp[j-1] , Max[j-1]) + a[j]; //dp[j]表示前j个数构成i个子段的最大和 
25                 Max[j-1] = temp; //Max[j-1]表示前j - 1个数构成 i - 1个子段的最大和 
26                 temp = max(temp,dp[j]);//temp做为中间值去记录Max[j-1], 方便循环的下一次更新Max[j-1] 
27             }
28         }
29         printf("%d\n",temp);
30     }
31     return 0;
32 }

猜你喜欢

转载自www.cnblogs.com/AaronChang/p/12221852.html