1007 Maximum Subsequence Sum (25分) 求最大连续区间和

1007 Maximum Subsequence Sum (25分)

 

Given a sequence of K integers { N1​​, N2​​, ..., NK​​ }. A continuous subsequence is defined to be { Ni​​, Ni+1​​, ..., Nj​​ } where 1ijK. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21
 

Sample Output:

10 1 4

题意:
  求连续区间最大和,并输出这个区间左右端点的值

题解:
  1、模拟,求出每一个正数的连续区间和,更新最大区间值并记录左右端点的值
  2、dp[i]表示区间以a[i]结尾的区间和,转移方程为dp[i]=max(dp[i],dp[i-1]+a[i]),并更新区间下标
注意:
  1、当a[i]全为负值的时候,输出0 a[0] a[n-1]
  2、
  当输入为:
  5
  -1 -1 0 -1 -1
  输出为:0 0 0
 
代码一(模拟)
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<algorithm>
#include<map>
#define MAX 1000000
#define ll long long
using namespace std;
int a[10005];
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    int ans=-1,l=0,r=n-1;
    int temp=0,first=0;
    for(int i=0;i<n;i++)
    {
        temp=temp+a[i];
        if(temp<0)
        {
            temp=0;
            first=i+1;
        }
        else if(temp>ans)
        {
            ans=temp;
            l=first;
            r=i;
        }
    }
    if(ans<0)
        ans=0;
    cout<<ans<<' '<<a[l]<<' '<<a[r]<<endl;
    return 0;
}

代码二(dp)
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<algorithm>
#include<map>
#define MAX 1000000
#define ll long long
using namespace std;
int a[10005],l[10005],r[10005];
int dp[10005];//以a[i]结尾的最大连续区间和
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        dp[i]=a[i];//初始化
    }

    l[0]=r[0]=0;

    for(int i=1;i<n;i++)
    {
        if(dp[i-1]+a[i]>=dp[i])//如果a[i]>=0
        {
            dp[i]=dp[i-1]+a[i];
            l[i]=l[i-1];//区间左端点不变,右端点更新
            r[i]=i;
        }
        else//如果a[i]是负数,新建一个区间
        {
            l[i]=i;
            r[i]=i;
        }
    }
    int ans=-1,pos=0,flag=0;
    for(int i=0;i<n;i++)
    {
        if(dp[i]>ans)
        {
            flag=1;
            ans=dp[i];
            pos=i;
        }
    }
    //cout<<flag<<endl;
    if(flag==0)
        cout<<0<<' '<<a[0]<<' '<<a[n-1]<<endl;
    else
        cout<<ans<<' '<<a[l[pos]]<<' '<<a[r[pos]]<<endl;
    return 0;
}
 

猜你喜欢

转载自www.cnblogs.com/-citywall123/p/12103282.html
今日推荐