HDU 1024 DP Max Sum Plus Plus

题意:本题的大致意思为给定一个数组,求其分成m个不相交子段和最大值的问题。

kuangbin专题。

dp[i][j]=Max(dp[i][j-1]+a[j] , max( dp[i-1][k] ) + a[j] ) 0<k<j

注意可以换成一维数组。第一维i没有影响,我们要求dp[m][n],所以i维可以省去,每次保留前j-1个数之中最大的部分。

 1 #include<iostream>
 2 #include<string>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<cstdio>
 6 #include<set>
 7 #include<map>
 8 #include<vector>
 9 #include<cstring>
10 #include<stack>
11 #include<cmath>
12 #include<queue>
13 #include <bits/stdc++.h>
14 using namespace std;
15 #define INF 0x3f3f3f3f
16 #define LL long long
17 #define clc(a,b) memset(a,b,sizeof(a))
18 int a[1000010];
19 int dp[1000010];
20 int maxn[1000010];
21 int main()
22 {
23     int m,n;
24     while(~scanf("%d%d",&m,&n))
25     {
26         for(int i=1; i<=n; i++)
27         {
28             scanf("%d",&a[i]);
29         }
30         clc(dp,0);
31         clc(maxn,0);
32         int maxx;
33         for(int i=1; i<=m; i++)
34         {
35             maxx=-INF;
36             for(int j=i; j<=n; j++)
37             {
38                 dp[j]=max(dp[j-1]+a[j],maxn[j-1]+a[j]);
39                 maxn[j-1]=maxx;
40                 maxx=max(maxx,dp[j]);
41             }
42         }
43         printf("%d\n",maxx);
44     }
45     return 0;
46 }

时间: 2024-10-10 22:27:17

HDU 1024 DP Max Sum Plus Plus的相关文章

HDU 1024:Max Sum Plus Plus(DP)

http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24675    Accepted Submission(s): 8478 Problem Description Now I think you have g

HDU 1003:Max Sum(DP)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 142742    Accepted Submission(s): 33225 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

杭电1024(Max Sum Plus Plus)

点击打开杭电1024 Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem. Given a consecutive

HDU1024——DP——Max Sum Plus Plus

Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem. Given a consecutive number sequ

杭电 HDU ACM 1003 Max Sum

 Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 164592    Accepted Submission(s): 38540 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the ma

HDU1003——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 contains

[VJ][DP]Max Sum Plus Plus

Max Sum Plus Plus Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem. Given a consecutive n

HDU acm 1003 Max Sum || 动态规划求最大子序列和详解

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 164592    Accepted Submission(s): 38540 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 [动态规划]

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1003 动态规划:f[i]表示以 i 结尾的最大的连续和.则转移方程为  f[1]=a[1]; if(f[i-1]+a[i]>=a[i]){ f[i]=f[i-1]+a[i]; } else {   f[i]=a[i]; } 再顺带更新一下起始终止位置就可以了. #include<cstdio> #include<iostream> #include<algorithm>