POJ2479,2593: 两段maximum-subarray问题

虽然是两个水题,但是一次AC的感觉真心不错

这个问题算是maximum-subarray问题的升级版,不过主要算法思想不变:

1. maximum-subarray问题

maximum-subarray就是找到数组A[1....n]中的连续子数组A[i.....j]并且A[i]+...+A[j]和最大。当然了,(1<=i<=j<=n)。

maximum-subarray的O(n)解法就是从左到右扫描数组A,另外设置一工具数组DP,DP数组的作用就是记录以当前下标为终止下标的子数组和。比如DP[j]=A[i]+....+A[j](A[i] 到 A[j]是连续的)。现在假设我们已经求出DP[j],数组即将扫描A[j+1],则DP[j]与DP[j+1]的关系描述如下:

DP[j+1]=(DP[j]>0)? (DP[j]+A[j+1]) : (A[j+1]).

因为DP[j+1]要求出以j+1下标为结束下标的子数组和,而且DP[j]已经求出,所以我们要判断DP[j]是否为正数,如为正,则加上A[j+1]。如为负,那么很明显的,A[i]+.....+A[j]+A[j+1]<A[j+1], 所以要让DP[j+1]=A[j+1]。

2. 求两个maximum-subarray问题

这两个maximum-subarray不相交。

我们可以设置一个“分水岭”,假设为k,那么maximum-subarray(A[1..k]) + maximum-subarray(A[k+1..n])就是我们要的解。

当然如果我们枚举每一个k值(1<=k<=n-1)的话,因为题目开出的N值为50000,真个时间复杂度为O(n^2),必然超时。

所以我们可以再设两个工具数组:lmax和rmax,lmax[i]表示A[1]到A[i]的最大子数组和,rmax[i]=A[i+1]....A[n]的最大子数组和。再次明确一下:lmax/rmax数组与DP数组的不同。

假设DP[s]=A[p+..q+..+r+..s], 那么lmax[s]可能就等于A[q+...+r]或者A[p+...+r]或者等等。

然后我们得到每个lmax[k]+rmax[k],对k进行枚举,lmax[k]+rmax[k]值最大的即为最后的解。

附上POJ2593代码:(POJ2479改动一点就可以了)

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
using namespace std;
const int max_size=100010;
int n,a[max_size];
int ldp[max_size],rdp[max_size],ans,inf=1<<30;
int lmax[max_size],rmax[max_size];
int main(){
    while(scanf("%d",&n)!=EOF&&n){
        memset(a,0,sizeof(a));
        memset(ldp,0,sizeof(ldp));
        memset(rdp,0,sizeof(rdp));
        memset(lmax,0,sizeof(lmax));
        memset(rmax,0,sizeof(rmax));
        ans=-inf;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
        }
        lmax[1]=ldp[1]=a[1];
        for(int i=2;i<=n;i++){
            if(ldp[i-1]>0) ldp[i]=ldp[i-1]+a[i];
            else ldp[i]=a[i];
            ans=max(ans,ldp[i]);
            lmax[i]=ans;
        }

        rmax[n]=rdp[n]=a[n];
        ans=-inf;
        for(int i=n-1;i>=1;i--){
            if(rdp[i+1]>0) rdp[i]=rdp[i+1]+a[i];
            else rdp[i]=a[i];
            ans=max(ans,rdp[i]);
            rmax[i]=ans;
        }
        ans=-inf;
        for(int k=1;k<=n-1;k++){
            ans=max(ans,lmax[k]+rmax[k+1]);
        }
        printf("%d\n",ans);
    }
}
时间: 2024-11-06 14:24:16

POJ2479,2593: 两段maximum-subarray问题的相关文章

[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

Lintcode42 Maximum Subarray II solution 题解

[题目描述] Given an array of integers, find two non-overlapping subarrays which have the largest sum.The number in each subarray should be contiguous.Return the largest sum. Notice:The subarray should contain at least one number 给定一个整数数组,找出两个 不重叠 子数组使得它们

LeetCode OJ平台上Maximum Subarray题目O(n)复杂度解决方案

原始题目如下,意为寻找数组和最大的子串,返回这个最大和即可. Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6.

[Lintcode] Best Time to Buy and Sell Stock I, II, III &amp;&amp; Maximum Subarray

买卖股票系列 && Subarray 相关题目: Best Time to Buy and Sell Stock I && II && III (IV 单独开贴) Maximum Subarray I && II 为什么这些我要总结到一起呢?因为思路基本一致. 题目简略: 买卖股票1: 一次买卖,利润最大. 买卖股票2: N次买卖,利润最大. 买卖股票3: 2次不重叠的买卖,利润最大. Maximum Subarray: Given an a

LeetCode 53. Maximum Subarray(最大的子数组)

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6. click to show more practice. Mor

Maximum Subarray II

Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Notice The subarray should contain at least one number For given [1, 3, -1, 2, -1, 2]

【leetcode】Maximum Subarray (53)

1.   Maximum Subarray (#53) Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2, 1, -3, 4, -1, 2, 1, -5, 4], the contiguous subarray [4, -1, 2, 1] has the largest s

leetCode 53.Maximum Subarray (子数组的最大和) 解题思路方法

Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6. click to show

Maximum Subarray leetcode java

题目: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have