2--最大子列和问题

本文介绍了如何使用Python解决给定整数序列的最大子列和问题,通过暴力法、分治法(Divide and Conquer)以及在线处理的解决方案,并解释了关键代码段。特别关注了特殊情况,如负数序列和子序列首尾元素的输出要求。
摘要由CSDN通过智能技术生成

 一、原型:

给定K个整数组成的序列{ N1​, N2​, ..., NK​ },“连续子列”被定义为{ Ni​, Ni+1​, ..., Nj​ },其中 1≤i≤j≤K。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13 }有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和。

输入格式:

输入第1行给出正整数K (≤100000);第2行给出K个整数,其间以空格分隔。

输出格式:

在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。

        法一:暴力

 发现没必要,对于每个j都要重算一遍->累加去比较即可

        法二: 分而治之

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

         法三:在线处理

                 个人solution:

#include<iostream>
using namespace std;
int Scanner(int*& a,int k)
{
    int MaxSum=0,ThisSum=0;
    for(int i=0;i<k;i++)
    {
        ThisSum+=a[i];
        if(ThisSum>MaxSum)
            MaxSum=ThisSum;
        else if(ThisSum<0)
            ThisSum=0;
    }
    return MaxSum;
}
int main()
{
    int K,ans;cin>>K;
    int *p=new int[K];
    for(int i=0;i<K;i++)
        cin>>p[i];
    ans=Scanner(p,K);
    cout<<ans;
    delete []p;
    p=nullptr;
    return 0;
}

 二、Maximum Subsequence Sum

        (2004年浙江大学计算机专业考研复试真题)

Given a sequence of K integers { N1​, N2​, ..., NK​ }. A continuous subsequence is defined to be { Ni​, Ni+1​, ..., Nj​ } 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

先吐槽一下 :

①翻译理解时,出了一点歧义,题目要求输出该子列的(正常,非max=0的情况)第一个数和最后一个数,(一开始我误以为是输出下标。。)

②看看下面这个样例

5

1 -1 0 2 2 

一开始我以为尽可能的小,i和j尽量靠近,但实际上不然,输出 4 1 2从1开始记数!!!

所以导致下面的this_sum<0不能取等号。 

个人题解:

#include<iostream>
using namespace std;

int i=1,j=1;
int i_real=0;
bool flag=0;/*先假设没找到*/
bool has_zero=0;int first_zero_index=0;

int Scan(int *&p,int k)
{
    int this_sum=0,max_sum=0;
    int count=0;
    for(int x=0;x<k;x++)
    {
        if(has_zero==0&&p[x]==0)
        {
            has_zero=1;
            first_zero_index=x;
        }
        this_sum+=p[x];
        count++;
        if(this_sum>max_sum)  //说明有正数
        {
            max_sum=this_sum;
            flag=1;
            j=x;
            i_real=j-count+1;
        }
        else if(this_sum<0)
        {
            this_sum=0;
            count=0;
        }
    }
    if(flag==0&&has_zero==0)
    { i=0;j=k-1; }
    else if(flag==0){i=j=(first_zero_index);}
    else{i=i_real;}
    return max_sum;
}
int main()
{
    /*数据准备*/
    int k,*p;cin>>k;p=new int[k];
    for(int y =0;y<k;y++)
    {
        cin>>p[y];
    }
    int ans=Scan(p,k);
    cout<<ans<<" "<<p[i]<<" "<<p[j];
    delete []p;p=nullptr;
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Ocean__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值