题目

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. easyui中多级表头,主表头不能添加field字段,否则不居中

    <th field="" width="120" align="center" align="center" co ...

  2. spring相关的maven依赖

    <properties> <springframework.version>5.0.4.RELEASE</springframework.version> < ...

  3. java ipv4校验正则字符串

    String IPV4_REGEX = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)) ...

  4. json 脚本入库的几种方法

    json 脚本入库的几种方法,见代码: #-*- encoding: utf-8 -*- #第一种mongodb入库 # from pymongo import * # import json # c ...

  5. filter的基本介绍和使用

    简介 过滤器是处在客户端和服务器资源之间的一到过滤网,我们可以根据具体的需求来对请求头和数据就行预处理,也可以对响应头和和数据进行后处理.例如Jsp, Servlet, 静态图片文件或静态 html ...

  6. java JDBC (八) 连接池 DBCP

    package cn.sasa.demo1; import javax.sql.DataSource; import org.apache.commons.dbcp2.BasicDataSource; ...

  7. swift一些常用系统方法的简化使用

    //获取Image func FImage(_ imageName:String) -> UIImage { return UIImage(named:imageName)! } //获取Url ...

  8. InnoDB体系架构

    MySQL支持插件式存储引擎,常用的存储引擎则是MyISAM和InnoDB,通常在OLTP(Online Transaction Processing 在线事务处理)中,我们选择使用InnoDB,所以 ...

  9. 报错解决——pytesseract.pytesseract.TesseractError: (1,’Error opening data file /usr/local/share/tessdata/eng.traineddata’)

    解决方法:(原文地址http://stackoverflow.com/questions/14800730/tesseract-running-error) $ wget https://tesser ...

  10. golang 中处理大规模tcp socket网络连接的方法,相当于c语言的 poll 或 epoll

    https://groups.google.com/forum/#!topic/golang-nuts/I7a_3B8_9Gw https://groups.google.com/forum/#!ms ...