题目

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. delphi中的 IntToHex()

    Delphi 自带函数 IntToHex 功能说明:该函数用于将“十进制”转换成“十六进制”.该函数有二个参数.第一个参数为要转换的十进制数据,第二个参数是指定使用多少位来显示十六进制数据. 参考实例 ...

  2. eclipse中tomcat无法加载spring boot

    转自: http://blog.csdn.net/u010797575/article/details/50517777 最近搭建一套spring boot框架,作为 application 启动项目 ...

  3. vue脚手架 构建豆瓣App 第一天

    课堂笔记: 项目结构分析: 项目入口:index.html(div#app) 全局vue组件:App.vue(template:div#app) 通过相同id的div,index.html与Appvu ...

  4. 注解之@CookieValue

    @RequestHeader以及@CookieValue这两个注解用法类似,属性也相同,所以,写在一起.二者属性和RequestParam的属性一样,用法也几乎一样. 作用 @RequestHeade ...

  5. R的常用命令

    mean(x) 求x的均值 sd(x) 求x的标准差 plot(x,y) 图形展示x和y的关系 ls() 查看当前所有加载到内存中的对象 rm(x) 删除当前内存中的对象x length(x) 求x的 ...

  6. centos下搭建sockets5代理

    #安装依赖及ss5 yum -y install gcc openldap-devel pam-devel openssl-devel wget https://nchc.dl.sourceforge ...

  7. .NET 三层框架

    1.三层架构 三层架构(3-tier architecture) 通常意义上的三层架构就是将整个业务应用划分为:表现层(Presentation layer).业务逻辑层(Business Logic ...

  8. css中 ~的作用

    这是 CSS3 element1~element2 选择器 定义和用法 element1~element2 选择器 element1 之后出现的所有 element2. 两种元素必须拥有相同的父元素, ...

  9. vue中indexDB的应用

    // indexedDB.js,浏览器本地数据库操作 export default { // indexedDB兼容 indexedDB: window.indexedDB || window.web ...

  10. 64位的pyd报"ImportError: No module named"错误

    今天仿照32位的ModuleA.cp35-win32.pyd,制作了64位的pyd.很随意地命名为ModuleA.cp35-win64.pyd,然后在64环境里编译.发现一直报"Import ...