[ACM] POJ 2479 Maximum sum (动态规划求不相交的两段子段和的最大值)
来源:互联网 发布:复合弓淘宝 编辑:程序博客网 时间:2024/06/05 14:20
Maximum sum
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 33363 Accepted: 10330
Description
Given a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:Your task is to calculate d(A).
Input
The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input.
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.
Output
Print exactly one line for each test case. The line should contain the integer d(A).
Sample Input
1101 -1 2 2 3 -3 4 -4 5 -5
Sample Output
13
Hint
In the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.
Huge input,scanf is recommended.
Huge input,scanf is recommended.
Source
POJ Contest,Author:Mathematica@ZSU
题意要求为给定一个数字序列,找出两段不相交的子段,使这两个子段的和最大,求出这个最大值。
dp[i]表示 从位置1到i 之间的最大子段和,正向求一遍。然后逆向求最大子段和,比如逆向求出当前位置i的最大字段和为sum,那么 ans= max( ans,dp[i-1]+sum), ans即为答案。
代码:
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;const int maxn=50010;const int inf=-0x7fffffff;int dp[maxn];int num[maxn];int t,n;void DP()//正向求最大子段和{ memset(dp,0,sizeof(dp)); int sum=inf,b=inf; for(int i=1;i<=n;i++) { if(b>0) b+=num[i]; else b=num[i]; if(b>sum) { sum=b; dp[i]=sum; } }}int main(){ scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&num[i]); DP(); int ans=inf,b=0,sum=inf;//逆向求n到i最大字段和,与正向的最大字段和相加,求出最大值 for(int i=n;i>1;i--) { if(b>0) b+=num[i]; else b=num[i]; if(b>sum) sum=b; if(sum+dp[i-1]>ans) ans=sum+dp[i-1]; } printf("%d\n",ans); } return 0;}
0 1
- [ACM] POJ 2479 Maximum sum (动态规划求不相交的两段子段和的最大值)
- [ACM] POJ 2479 Maximum sum (动态规划求不相交的两段子段和的最大值)
- POJ 2479 Maximum sum (求2个不相交的连续字段和的最大值)
- poj 2479 求两段子数组和相加的最大值
- POJ 2479 Maximum sum(dp—求最大的两个不相交的字段和)
- poj2479 Maximum sum 求最大的 数列中不相交的俩个子段和
- 1486【动态规划★★】Max Sum Plus Plus.(求n个数m个不相交子段的最大和)
- POJ 2479 动态规划 最大子序列问题(两段) Maximum sum
- hdu 1024 Max Sum Plus Plus(dp求m个不相交子段和的最大值)
- 经典动态规划——HDU1024 m段子段和的最大值
- hdu 1024 Max Sum Plus Plus (求一个序列中选出的m个不相交子段和的最大值)
- poj 2479 Maximum sum(求最大子段和的延伸)
- poj 2479 Maximum sum(求最大子段和的延伸)
- 【动态规划】poj2479 Maximum sum && poj2593 Max Sequence(求两段不相交字串的最大和)
- poj 2479 Maximum sum 两段不相交最大子段和
- hdu 1024 Max Sum Plus Plus(动态规划+m子段和的最大值)
- A - Max Sum Plus Plus HDU1024 ( 动态规划 多段连续子段和的最大值)
- poj 2479 dp Maximum sum求两段连续不重叠子段的最大和
- hdu 4027 Can you answer these queries? 线段树
- hdu 1130 How Many Trees?(二叉树节点的Catalan数)
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions
- JQuery函数整理
- 如何在ANDROID JNI 的C++中打Log
- [ACM] POJ 2479 Maximum sum (动态规划求不相交的两段子段和的最大值)
- 二叉树(二)——遍历、深度统计、叶子结点统计、结点统计
- 关于matlab中struct增长的优化
- C/C++日常学习总结(第十三篇)默认构造函数内部再调用带参的构造函数
- 123
- 划分树模板
- TreeView常用设置
- hd 2030 汉字统计
- Gradle-user guide-Installing-第四章 安装Gradle(译)