POJ 2479 Maxmimum sum & 2593 Max Sequence

2479 Maxmimum sum

题目描述

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).

输入

The input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input.
Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, …, an. (|ai| <= 10000).There is an empty line after each case.

输出

Print exactly one line for each test case. The line should contain the integer d(A).

2593 Max Sequence

题目描述

Give you N integers a1, a2 … aN (|ai| <=1000, 1 <= i <= N).
在这里插入图片描述
You should output S.

输入

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.

输出

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

题目解析

方法一

  • 使用for循环将序列分为a[0]-a[i]和a[i+1]-a[n-1]两个部分,针对两个部分分别求最大字段和并相加,最终将和最大的输出。
  • 但是这种方法的时间复杂度为O( n 2 n^{2} n2),在OJ上会超时,所以需要寻找时间复杂度更低的方法

方法二

  • 通过分析,我们可以发现,前一个子段和可以通过公式得出,后一个子段和可以视为从后往前求子段和,那么这样一来,我们就可以把方法一中利用for循环划分序列的部分删去,使得时间复杂度降低为O(n)
  • 我们使用a数组来保存序列,b数组来保存正向子段和,c数组来保存反向子段和
  • 在求反向子段和时,使用变量tmp保存到目前为止反向子段和的最大值
  • 那么在求反向子段和的for循环中,就可以通过ans=max(ans,b[i]+tmp);求得最终结果了(我好像写的并不简单易懂,请看代码和注释吧)

求最大字段和思路

求前i个元素的最大子段和b[i]时,分为两种情况:

  1. 继承顺延:前i-1个元素的最大字段和b[i-1]加上当前元素a[i],即b[i]=b[i-1]+a[i]
  2. 另起炉灶:直接使用当前元素a[i]作为前i个元素的最大子段和b[i],即b[i]=a[i]
  3. 当i=0时,即只有一个元素时,b[i]=a[i]

综上所述,可以得到公式
b [ i ] = { a [ i ] i = 0 max ⁡ { a [ i ] , b [ i − 1 ] + a [ i ] } i ! = 0 b[i] = \begin{cases} a[i] & i=0 \\ \max\{ a[i],b[i-1]+a[i] \} & i!=0 \end{cases} b[i]={a[i]max{a[i],b[i1]+a[i]}i=0i!=0

AC代码

2479 Maxmimum sum

#include <iostream>
#define INF 0x3f3f3f3f
#define MAX 50000
int a[MAX],b[MAX],c[MAX];   //a保存序列,b保存正向子段和,c保存反向子段和
using namespace std;
int main()
{
    int T;   //测试数据组数
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0;i<=n-1;i++)
        {
            scanf("%d",&a[i]);
            b[i]=c[i]=-INF;
        }
        int ans=-INF;
        b[0]=a[0];   //当子段长度为1时的情况
        c[n-1]=a[n-1];   //由于c是反向的,所以不是0,而是n-1
        for(int i=1;i<=n-1;i++)
        {
            b[i]=max(b[i-1]+a[i],a[i]);   //求正向子段和
        }
        int tmp=a[n-1];   //使用tmp保存反向子段和的最大值
        for(int i=n-2;i>=0;i--)
        {
            c[i]=max(c[i+1]+a[i],a[i]);   //求反向子段和
            tmp=max(tmp,c[i+1]);   //更新tmp,使用c[i+1]是为了避免子段重合,详见下一行
            ans=max(ans,b[i]+tmp);
            //更新ans,此处的b[i]表示0~n-1的最长子段和,tmp表示i+1~n-1的最长子段和,确保不会重合
        }
        printf("%d\n",ans);
    }
    return 0;
}

2593 Max Sequence

#include <iostream>
#define INF 0x3f3f3f3f
#define MAX 101000
int a[MAX],b[MAX],c[MAX];
using namespace std;
int main()
{
    int n;
    while(scanf("%d",&n))
    {
        if(n==0)
        {
            break;
        }
        for(int i=0;i<=n-1;i++)
        {
            scanf("%d",&a[i]);
            b[i]=c[i]=-INF;
        }
        int ans=-INF;
        b[0]=a[0];
        c[n-1]=a[n-1];
        for(int i=1;i<=n-1;i++)
        {
            b[i]=max(b[i-1]+a[i],a[i]);
        }
        int tmp=a[n-1];
        for(int i=n-2;i>=0;i--)
        {
            c[i]=max(c[i+1]+a[i],a[i]);
            tmp=max(tmp,c[i+1]);
            ans=max(ans,b[i]+tmp);
        }
        printf("%d\n",ans);
    }
    return 0;
}

结果

代码AC
注意:

  • 需要使用scanf,如果使用cin则会TLE
  • 两道题的思路相同,但是输入形式不同,数据范围也不同,一个是50000,一个是100000
    在这里插入图片描述在这里插入图片描述
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值