poj 2479 dp求分段最大和

Maximum sum

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 38079   Accepted: 11904

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.

Output

Print exactly one line for each test case. The line should contain the integer d(A).

Sample Input

1

10
1 -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.

题目大意:将区间分成了两部分,让你求这两部分的和最大是多少。

思路分析:前面做过求最大连续序列和的题目,是用dp做的,状态转移方程为dp[i]=max[dp[i-1]+a[i],a[i])

这道题与那道题很像,唯一的区别就是区间变成了两部分,现在要求和最大,dp[i]的意思是以i结尾的子串的最大

和,现在可以把分成求两个dp,l[i]代表i之前的最大和,r[i]代表i之后的最大和,题目要求的不就是求l[i-1]+r[i]的

最大值么,现在问题就变成了如何求了l[i]和r[i],状态转移方程很容易确定,l[i]=max(l[i-1],t1[i]),t1[i]指的是
以i个数结尾的最大子串和,这样问题经过几步分解就变成了已经解决过的问题了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int maxn=50000+100;
int a[maxn],t1[maxn],t2[maxn],l[maxn],r[maxn];
const int inf=-1000001;
int main()
{
        int T;
        scanf("%d",&T);
        while(T--)
        {
            int n;
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
            }
            memset(t1,0,sizeof(t1));
            memset(t2,0,sizeof(t2));
            for(int i=1;i<=n;i++)
            {
                t1[i]=max(t1[i-1]+a[i],a[i]);
            }
            for(int i=n;i>=1;i--)
            {
                t2[i]=max(t2[i+1]+a[i],a[i]);
            }
            l[1]=a[1];
            for(int i=2;i<=n;i++)
            {
                l[i]=max(l[i-1],t1[i]);
            }
           r[n]=a[n];
           for(int i=n-1;i>=1;i--)
           {
               r[i]=max(r[i+1],t2[i]);
           }
           int ma=inf;
           for(int i=2;i<=n;i++)
           {
               int t=l[i-1]+r[i];
               ma=max(ma,t);
           }
           cout<<ma<<endl;
        }
     return 0;
}

时间: 2024-08-05 07:03:41

poj 2479 dp求分段最大和的相关文章

POJ 2096 (dp求期望)

A - Collecting Bugs Time Limit:10000MS     Memory Limit:64000KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-05-15) Description Ivan is fond of collecting. Unlike other people who collect post stamps, co

Poj 2096 (dp求期望 入门)

/ dp求期望的题. 题意:一个软件有s个子系统,会产生n种bug. 某人一天发现一个bug,这个bug属于某种bug,发生在某个子系统中. 求找到所有的n种bug,且每个子系统都找到bug,这样所要的天数的期望. 需要注意的是:bug的数量是无穷大的,所以发现一个bug,出现在某个子系统的概率是1/s, 属于某种类型的概率是1/n. 解法: dp[i][j]表示已经找到i种bug,并存在于j个子系统中,要达到目标状态的天数的期望. 显然,dp[n][s]=0,因为已经达到目标了.而dp[0][

POJ 2479 两段连续最大和

题目大意: 在一组数中,找到连续的两段 , 是这两段相加和达到最大 这里利用dp[2][N]的数组保存所有的状态 dp[0][i]表示取到第i个数时只取了一段的最大和,第i个数是一定要被取到的 dp[1][i]表示取到第i个数时取了2段的最大和,第i个数是一定要被取到的 而题目所求答案就是所有dp[1][i]中的最大值 状态转移方程: dp[0][i] = max{dp[0][i-1]+a[i] , a[i]} dp[1][i] = max{dp[0][j]+a[i] , dp[1][i-1]+

[ACM] POJ 2479 Maximum sum (动态规划求不相交的两段子段和的最大值)

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total 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 o

poj 1655 树形dp求取树的重心

http://poj.org/problem?id=1655 Description Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the tree yields a forest: a collection of one or more trees. Define the balance of a node to be the size of the large

POJ 2096 Collecting Bugs(概率DP求期望)

传送门 Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 4333 Accepted: 2151 Case Time Limit: 2000MS Special Judge Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other material stu

HDU 1087 &amp;&amp; POJ 2533(DP,最长上升子序列).

~~~~ 两道题的意思差不多,HDU上是求最长上升子序列的和,而POJ上就的是其长度. 貌似还有用二分写的nlogn的算法,不过这俩题n^2就可以过嘛.. ~~~~ 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1087 http://poj.org/problem?id=2533 ~~~~ HDU1087: #include<cstdio> #include<cstring> #include<algorithm> #

POJ 3670 &amp;&amp; POJ 3671 (dp)

最长不下降子序列的应用嘛.两题都是一样的. POJ 3670:求给定序列按递增或递减排列时,所需改变的最小的数字的数目. POJ 3671:求给定序列按递增排列时,所需改变的最小的数字的数目. 思路就是求最长不下降子序列,然后剩下的就是需要改变的字母. 最长不下降子序列:(我之前有写过,不懂请戳)http://blog.csdn.net/darwin_/article/details/38360997 POJ 3670: #include<cstdio> #include<cstring

树形dp求树的重心

Balancing Act http://poj.org/problem?id=1655 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 #define mt(a,b) memset(a,b,sizeof(a)) 6 using namespace std; 7 const int M=50010; 8 vector<int> g[