[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 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.

Source

POJ Contest,Author:[email protected]

解题思路:

题意要求为给定一个数字序列,找出两段不相交的子段,使这两个子段的和最大,求出这个最大值。

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;
}

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

时间: 2025-01-04 05:41:34

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

POJ 2479 Maximum sum

http://poj.org/problem?id=2479 题意: 给出一个整数串,求连续子串1和连续子串2,不相交并且串1加串2的和最大. 思路: 其实就是求最大连续和,题意要求就是求两段最大连续和.我们可以从左边和右边分别求最大连续和,代码中的dp_l[i]就是1~i的最大连续和,dp_r[i]则是i~n的最大连续和. 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<

POJ 2479 Maximum sum(双向DP)

Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36100   Accepted: 11213 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 2479 Maximum sum【双向DP/最大连续和】

Maximum sum 题意:给定一个长度为N的数组,求两个连续的子序列,使得两个连续子序列的和最大. 分析:乍一看,跟最大连续和有点类似,但是,又有区别,因为对于这个题,考虑第i项两个连续子序列的最大和,不能仅仅由前i-1项递推得出,第i项两个连续子序列的最大和,与前i项和i以后的之间是存在关系的,因此这个题目是一个双向dp. 假如给定的序列为a0, a1, a2, a3, a4, ...... ,an,那么,对于任意第i项,我以第i个元素为分界点,用一个数组项pMax [i]来保存  区间

poj 2479 Maximum sum(递推)

?? 题意:给定n个数,求两段连续不重叠子段的最大和. 思路非常easy.把原串划为两段.求两段的连续最大子串和之和,这里要先预处理一下,用lmax数组表示1到i的最大连续子串和,用rmax数组表示n到i的最大连续子串和,这样将时间复杂度降为O(n). #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include&l

POJ 2479 Maximum sum ( DP )

题目大意: 对整数串S,求其两个不相交的子串s1.s2,使得s1+s2的值最大. 方法:DP, lt[i]代表以第i个元素结尾的串最大值 rt[i]代表以第i个元素开头的串的最大值 那么设置一个rtm[i]代表取后i个元素之中最大连续子串的和 很显然,lt[i]=max(a[i],lt[i-1]+a[i]); rt[i]=max(a[i],rt[i+1]+a[i]); rtm[i]=max(rtm[i+1],rt[i]); #include <iostream> #include <cs

[poj 2479] Maximum sum -- 转载

转自 CSND 想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410                                      转载请注明出处:http://blog.csdn.net/wangjian8006 题目大意: 对于连续的整数和的串s1和s2,s1与s2不相交,使得s1+s2最大 解题方法: DP.  lt[i]代表以第i个元素结尾的串最大值  rt[i]代表以第i个元素开头的串的最大

[ACM] POJ 2593 Max Sequence (动态规划,最大字段和)

Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 15569   Accepted: 6538 Description Give you N integers a1, a2 ... aN (|ai| <=1000, 1 <= i <= N). You should output S. Input The input will consist of several test cases. Fo

Leetcode 1191 K-Concatenation Maximum Sum 动态规划

Leetcode 1191 K-Concatenation Maximum Sum 动态规划 题目描述 Given an integer array arr and an integer k, modify the array by repeating it k times. For example, if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2]. Return the maximum s

URAL 1146. Maximum Sum(求最大子矩阵和)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1146 1146. Maximum Sum Time limit: 0.5 second Memory limit: 64 MB Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the largest sum. The sum of a rectangle is