[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. For each test case, one integer N (2 <= N <= 100000) is given in the first line. Second line contains N integers. The input is terminated by a single line with N = 0.

Output

For each test of the input, print a line containing S.

Sample Input

5
-5 9 -5 11 20
0

Sample Output

40

Source

POJ Monthly--2005.08.28,Li Haoyuan

解题思路:

同 POJ 2479http://blog.csdn.net/sr_19930829/article/details/38397435

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

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=100010;
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()
{
    while(scanf("%d",&n)!=EOF&&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 2593 Max Sequence (动态规划,最大字段和)

时间: 2024-12-16 09:11:52

[ACM] POJ 2593 Max Sequence (动态规划,最大字段和)的相关文章

[ACM] POJ 1141 Brackets Sequence (区间动态规划)

Brackets Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25087   Accepted: 7069   Special Judge Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a regular sequence. 2. If S is a re

[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 2593&amp;&amp;2479:Max Sequence

Max Sequence Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16329   Accepted: 6848 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

poj2593 Max Sequence(求两个不相交最大字段和)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=2593 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. For e

[ACM] POJ 2442 Sequence (堆的性质)

Sequence Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7011   Accepted: 2262 Description Given m sequences, each contains n non-negative integer. Now we may select one number from each sequence to form a sequence with m integers. It's

[ACM] POJ 2677 Tour (动态规划,双调欧几里得旅行商问题)

Tour Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3585   Accepted: 1597 Description John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must

[ACM] POJ 1442 Black Box (堆,优先队列)

Black Box Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7099   Accepted: 2888 Description Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empt

[ACM] POJ 1068 Parencodings(模拟)

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn

[ACM] poj 2823 Sliding Window(单调队列)

Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 36212   Accepted: 10723 Case Time Limit: 5000MS Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left