题目

https://pintia.cn/problem-sets/900290821590183936/problems/900291257604861953

给出一段数列,求数列的最大子列和,并输出子列和的首尾元素。例如给出序列{ -2, 11, -4, 13, -5, -2 },最大的子列是{11,-4,13},应当输出{20,11,13}。如果有多个子列和相同,输出索引最小的那个。

如果最大子列和是负的则认为是0.

Sample Input:

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

Sample Output:

10 1 4

分析

使用线性搜索找到最大子列和,当更新最大值的时候顺便更新left和right。当子列和是负的时候,更新起始位置到临时变量t

AC代码

#include "bits/stdc++.h"
using namespace std;
int main(int argc, char const *argv[])
{
int k, i, a[10010];
cin >> k;
for(i = 0; i<k; i++) cin >> a[i];
int mmax = 0, sum = 0;
int t=k, l=0, r=k;
for(i=k-1;i>=0;i--){
sum += a[i];
if(sum>=mmax){
mmax = sum;
l = i;
r = t;
}
if(sum <= 0){
sum = 0;
t = i;
}
}
cout << mmax << ' ' << a[l] << ' ' << a[r-1]; return 0;
}

Maximum-SubsequenceSum的更多相关文章

  1. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  2. Uncaught RangeError: Maximum call stack size exceeded 调试日记

    异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...

  3. iOS---The maximum number of apps for free development profiles has been reached.

    真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...

  4. MTU(Maximum transmission unit) 最大传输单元

    最大传输单元(Maximum transmission unit),以太网MTU为1500. 不同网络MTU如下: 如果最大报文数据大小(MSS)超过MTU,则会引起分片操作.   路径MTU: 网路 ...

  5. uva 11059 maximum product(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK

  6. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  7. [LeetCode] Third Maximum Number 第三大的数

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

  8. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  9. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  10. [LeetCode] Maximum Product of Word Lengths 单词长度的最大积

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

随机推荐

  1. py文件传输

    本文参考:http://blog.163.com/kongdelu2009@yeah/blog/static/1119952072009102562126194/ 发送端程序: # -*- codin ...

  2. Oracle shrink table

    shrink必须开启行迁移功能. alter table table_name enable row movement ; 在oracle中可以使用alter table table_name shr ...

  3. LeetCode 806 Number of Lines To Write String 解题报告

    题目要求 We are to write the letters of a given string S, from left to right into lines. Each line has m ...

  4. Servlet基本介绍和使用

    基本概念 Servlet又称为Java Servlet是一个基于java技术的web组件,运行在服务器端,用于生成动态的内容.Servlet是平台独立的java类,编写一个Servlet实际上就是按照 ...

  5. C#实体对象出现中文处理乱码的问题

    问题: C#实体对象使用时,对于是中文的增加时,到数据库后变成了?? 解决方法: 增加实体对象时“新建连接”操作中的高级中要设置属性Character Set=urf8  (要手动输入)

  6. EscapeDataString URI 字符串太长

    /// <summary> /// 处理 无效的 URI: URI 字符串太长.问题 /// </summary> /// <param name="value ...

  7. Redis入门到高可用(十二)—— pipeline

    一.回忆通信模型 二.流水线 1.什么是流水线 2.pipeline-Jedis实现 3.与原生M(mget,mset等)操作对比 M操作是原子操作 pipeline命令是非原子的,Redis服务器会 ...

  8. OpenVPN简介及架构详解

    OpenVPN简介 1 简介    VPN(Virtual Private Network)直译就是虚拟专用通道,是提供给企业之间或者个人与公司之间安全数据传输的隧道. OpenVPN无疑是Linux ...

  9. sap 创建odata服务,通过http向数据库 进行增删改查

    https://blog.csdn.net/stone0823/article/details/71057172 1:通过 事物码 se11 创建 数据库表  zemp.表 zemp中 含有empid ...

  10. 部分还款-还款试算接口-python

    一.swagger-ui中 二.python中调用接口,出现的问题: 解决办法: import requests # 还款试算接口 ur1='http://10.253.43.83:8399/repa ...