Maximum-SubsequenceSum
题目
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的更多相关文章
- POJ3693 Maximum repetition substring [后缀数组 ST表]
Maximum repetition substring Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9458 Acc ...
- Uncaught RangeError: Maximum call stack size exceeded 调试日记
异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...
- 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.免费应用程序调试最 ...
- MTU(Maximum transmission unit) 最大传输单元
最大传输单元(Maximum transmission unit),以太网MTU为1500. 不同网络MTU如下: 如果最大报文数据大小(MSS)超过MTU,则会引起分片操作. 路径MTU: 网路 ...
- uva 11059 maximum product(水题)——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK
- [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 ...
- [LeetCode] Third Maximum Number 第三大的数
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- [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 ...
- [LeetCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- [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 ...
随机推荐
- Mysql undo redo 总结
- LeetCode 908 Smallest Range I 解题报告
题目要求 Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K ...
- (转载)centos7启用端口
转载:原文地址:https://www.cnblogs.com/moxiaoan/p/5683743.html 1.firewalld的基本使用 启动: systemctl start firew ...
- 解决采集知乎数据时由于账号被封遗漏的账号重爬问题(python代码)
'''一.最笨的办法了################################################################为了处理由于账号被封而没跑到的问题id进行以下两步 ...
- FPGA设计中的异步复位、同步释放思想
1.一个简单的异步复位例子: module test( input clk, input rst_n, input data_in, output reg out ); always@(posedge ...
- 安全需求-建模归类——By Me
漏洞与Bug并不等同,他们之间的关系基本可以描述为:大部分的Bug影响功能性,并不涉及安全性,也就不构成漏洞:大部分的漏洞来源于Bug,但并不是全部,它们之间只是有一个很大的交集.可以用如下这个图来展 ...
- 如何快速REPAIR TABLE
早上到公司,刚准备吃早餐,手机响了,一看是服务器自动重启了.好吧,准备修复数据吧.游戏服的游戏日志使用的是MyISAM.众所周知,MyISAM表在服务器意外宕机或者mysqld进程挂掉以后,MyISA ...
- redgate的mysql架构比较和数据比较工具
redgate的mysql架构比较和数据比较工具 最近线上数据需要进行架构比较,比较两个服务器上的mysql实例上数据库的架构 数据比较可以用percona的pt-table-checksum和pt- ...
- MySQL无损复制(转)
MySQL5.7新特性:lossless replication 无损复制 https://dev.mysql.com/doc/refman/5.7/en/replication-semisync.h ...
- pycharm 如何设置方法调用字体颜色
一.pycharm 如何设置方法调用字体颜色 1.打开pycharm编辑器,file > settings > editor > color scheme > python & ...