「1007」 Maximum Subsequence Sum

➳ENTRY

Given a sequence of K integers . A continuous subsequence is defined to be where . The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence , its maximum subsequence is 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

Ω

最大子序和,顾名思义,求出和最大的连续子序列。

这是一道非常经典的动态规划问题,据说也是很多大厂的面试题,不过现在应该已经被做烂了,因为之前我就已经碰到过三四回了。对,不是这一类题目,是这一道题目。但我初看题目的时候还是有点懵,估计是之前没想透吧,那么今天就让它永久住在我的博客里,以加深印象。

说实话一开始的思路必然是有的,第一个想法必然是枚举所有的子序和,野蛮又暴力。作为一个受过高等教育的科班大学生,我们要文明一点。当然不是说枚举不好,有时候枚举也会有意想不到的奇效,but not now.

仔细分析一下最大子序列,我们不妨设最大子序列为 ,那么为什么 能成为最大子序列呢?首先
的任意连续子序列和必然小于 之和。另外 的两端要么没有数要么就是负数,不然 向两边扩张还可以变大,这与其定义“最大”矛盾。

由此我们其实可以得到一个非常重要的推论—— 前n个数之和必然是 的,否则丢弃前n个数得到新的子序列之和将会大于 之和。那么我们便可以设置一个sum=0变量,依次读入每一个数并计入sum中,若sum<0了根据之前的推论说明前面计入sum的序列不可能成为最大子序列的前置部分,因此直接令sum=0丢弃前面读入的序列和,直到再次读入一个正数后再开始计数。只要sum>0就与最大值进行比较,记录下最大的sum


C☺DE

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int n, sum = 0, max_sum = -1, former = 0, latter, former_tmp = 0; 
  //former,latter记录最大子序列的首位index,
  //max_sum=-1考虑0的存在,former_tmp必须赋初值(考虑第一个数是0的情况)
    cin >> n;
    latter = n - 1; //全为负数时输出序列的首位两个数
    vector<int> seq(n);
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &seq[i]);
        sum += seq[i];
        if (sum < 0)
        {
            sum = 0;
            former_tmp = i + 1; //former_tmp定位可能的最大子序列头index
        }
        else if (sum > max_sum)
        {
            max_sum = sum;
            former = former_tmp;
            latter = i;
        }
    }
    max_sum = (max_sum < 0 ? 0 : max_sum);//考虑全为负数的情况
    cout << max_sum << " " << seq[former] << " " << seq[latter];
}

Appendix(无需输出最大子序列头尾版本)

#include <iostream>

using namespace std;

int main()
{
    int n, m, sum = 0, max_sum = 0;
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        cin >> m;
        sum = ((sum += m) > 0 ? sum : 0);
        max_sum = (sum > max_sum ? sum : max_sum);
    }
    cout << max_sum;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值