Pat A1007

心得:在0和负数那个点卡了许久,原因是maxSum赋初值为0,若最大子序列和恰为0,则头尾信息无法得到更新。

1007 Maximum Subsequence Sum (25 分)

Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1≤i≤j≤K. 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

详细代码如下:

#include <cstdio>

//O(N^3)  --最后一个点超时
int MaxSubseqSum1(int a[], int n, int &head, int &tail)
{
    int thisSum, maxSum = -1;
    head = a[0];
    tail = a[n - 1];
    for (int i = 0; i < n; i++)     //子列左端位置
    {
        for (int j = i; j < n; j++) //子列右端位置
        {
            thisSum = 0;        //当前子列和初始为0
            for (int k = i; k <= j; k++)    //子列和从a[i]加到a[j]
            {
                thisSum += a[k];
            }
            //若当前子列和大于最大子列和,则将值保存给最大子列和
            if (thisSum > maxSum)
            {
                maxSum = thisSum;
                head = a[i];
                tail = a[j];
            }
        }
    }
    return maxSum;
}

//O(N^2)
int MaxSubseqSum2(int a[], int n, int &head, int &tail)
{
    int thisSum, maxSum = -1;
    head = a[0];
    tail = a[n - 1];
    for (int i = 0; i < n; i++)     //子列左端位置
    {
        thisSum = 0;
        for (int j = i; j < n; j++) //子列右端位置
        {
            thisSum += a[j];
            //若当前子列和大于最大子列和,则将值保存给最大子列和
            if (thisSum > maxSum)  //最大子列和为0时也要更新头尾
            {
                maxSum = thisSum;
                head = a[i];
                tail = a[j];
            }
        }
    }
    return maxSum;
}

//O(N) --在线处理
int MaxSubseqSum4(int a[], int n, int &head, int &tail)
{
    int thisSum = 0, maxSum = -1, currentHead = a[0];
    head = currentHead;     //列头初始化为整个数列第一个元素
    tail = a[n - 1];        //列尾初始化为整个数列最后一个元素
    for (int i = 0; i < n; i++)
    {
        thisSum += a[i];            //向右累加
        if (thisSum > maxSum)
        {
            maxSum = thisSum;
            head = currentHead;
            tail = a[i];
        }
        else if (thisSum < 0)
        {
            //发现子序列为负,则不可能使得后面的子序列更大,弃之
            thisSum = 0;
            currentHead = a[i + 1];
        }
    }
    return maxSum;
}

//O(NlogN) --分而治之(尚待完善)
int Max3( int A, int B, int C )
{ /* 返回3个整数中的最大值 */
    return A > B ? A > C ? A : C : B > C ? B : C;
}

int DivideAndConquer( int List[], int left, int right )
{ /* 分治法求List[left]到List[right]的最大子列和 */
    int MaxLeftSum, MaxRightSum; /* 存放左右子问题的解 */
    int MaxLeftBorderSum, MaxRightBorderSum; /*存放跨分界线的结果*/

    int LeftBorderSum, RightBorderSum;
    int center, i;

    if( left == right )  { /* 递归的终止条件,子列只有1个数字 */
        if( List[left] > 0 )  return List[left];
        else return 0;
    }

    /* 下面是"分"的过程 */
    center = ( left + right ) / 2; /* 找到中分点 */
    /* 递归求得两边子列的最大和 */
    MaxLeftSum = DivideAndConquer( List, left, center );
    MaxRightSum = DivideAndConquer( List, center+1, right );

    /* 下面求跨分界线的最大子列和 */
    MaxLeftBorderSum = 0; LeftBorderSum = 0;
    for( i=center; i>=left; i-- ) { /* 从中线向左扫描 */
        LeftBorderSum += List[i];
        if( LeftBorderSum > MaxLeftBorderSum )
            MaxLeftBorderSum = LeftBorderSum;
    } /* 左边扫描结束 */

    MaxRightBorderSum = 0; RightBorderSum = 0;
    for( i=center+1; i<=right; i++ ) { /* 从中线向右扫描 */
        RightBorderSum += List[i];
        if( RightBorderSum > MaxRightBorderSum )
            MaxRightBorderSum = RightBorderSum;
    } /* 右边扫描结束 */

    /* 下面返回"治"的结果 */
    return Max3( MaxLeftSum, MaxRightSum, MaxLeftBorderSum + MaxRightBorderSum );
}

int MaxSubseqSum3( int List[], int N )
{ /* 保持与前2种算法相同的函数接口 */
    return DivideAndConquer( List, 0, N-1 );
}

int main()
{
    int n, head, tail, result;
    int a[10086];
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    result = MaxSubseqSum4(a, n, head, tail);
    printf("%d %d %d", (result < 0? 0 : result), head, tail);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值