最大子序列和 o(n)
问题: 给定一整数序列A1, A2,... An (可能有负数),求A1~An的一个子序列Ai~Aj,使得Ai到Aj的和最大
例如:整数序列-2, 11, -4, 13, -5, 2, -5, -3, 12, -9的最大子序列的和为21。
从左到右记录当前子序列的和sum,开始位置为start,结束位置为end。若sum不断增加,那么最大子序列的和max也不断增加(不断更新max,start,end)。如果往前扫描中遇到负数,那么当前子序列的和将会减小。此时sum 将会小于max,当然max也就不更新。如果sum降到0时,说明前面已经扫描的那一段就可以抛弃了,这时将sum置为0,并且start为下一个位置。然后,sum将从后面开始将这个子段进行分析,若有比当前max大的子段,继续更新max。这样一趟扫描结果也就出来了。
import java.util.Scanner;
public class Test16 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
boolean negativeAll=true;
int n=sc.nextInt();
int[] arr = new int[n];
for(int i=0;i<n;i++){
arr[i]=sc.nextInt();
if(arr[i]>=0){
negativeAll=false;
}
}
if(negativeAll){
System.out.println(0+" "+arr[0]+" "+arr[n-1]);
}
else{
int sum=0,stmp=0,start=0,end=0,imax=-1;
for(int i=0;i<n;i++){
sum+= arr[i];
if(sum>imax){
start=stmp;
end=i;
imax=sum;
}
if(sum<0){
sum=0;
stmp=i+1;
}
}
System.out.println(imax+" "+arr[start]+" "+arr[end]);
}
}
}
// 1 -2 3 10 -4 7 2 -5
最大子序列和 o(n)的更多相关文章
- 用python实现最长公共子序列算法(找到所有最长公共子串)
软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Is Subsequence 是子序列
Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- [Data Structure] LCSs——最长公共子序列和最长公共子串
1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...
- 51nod1134(最长递增子序列)
题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1134 题意: 中文题诶~ 思路: 直接暴力的话时间复杂度为 ...
随机推荐
- TFS2012团队管理基本配置及基础使用方法
本文介绍如何在VS2012中使用微软提供的TFS2012服务器进行团队协作开发,免费默认只支持5用户,主要分为两大步服务器配置跟客户端配置. 转载请标注:http://www.kwstu.com/Ar ...
- MVC ActionResult视图结果
摘要 MVC框架针对HttpResponse进行抽象与多态,使HttpResponse均可表示为ActionResult.那么,抽象和多态表现在哪里呢? //封装一个Action的结果. public ...
- Unity Built-in Shader详解三
上次讲的是Transparent Shader Family,他们是绘制半透明的对象使用的,但他们并不能满足我们全部的要求. Transparent Cutout Shader Family是对半透明 ...
- OGRE: "OgreOverlaySystem.h": No such file or directory
这两天学习OGRE,遇到"OgreOverlaySystem.h": No such file or directory的错误. 这是由于OGRE提供的例子过老,和SDK版本不一致 ...
- 【原】storm源码之storm代码结构【译】
说明:本文翻译自Storm在GitHub上的官方Wiki中提供的Storm代码结构描述一节Structure of the codebase,希望对正在基于Storm进行源码级学习和研究的朋友有所帮助 ...
- css3 flex流动自适应响应式布局样式类
1.再说css3 flex 一旦一个容器赋予了display:flex属性,将会有以下特点: 项目无法设置浮动. 列表的样式会被清除. 无法使用vertical-align设置垂直对齐方式. 目前互联 ...
- 使用 Java 开发并生成 .jar 文件
1. 编写文件 D:\test\Hello.java: package test; public class Hello { public static void main(String argv[] ...
- 原创Java版的Shell
如果你接触过windows操作系统,你应该对windows中的cmd有一定的了解. 如果你接触过Linux操作系统,你应该对Linux的shell有一定的了解. 本文说的正是linux中的shell. ...
- MySQL添加字段和修改字段的方法
添加表字段 alter table table1 add transactor varchar(10) not Null; alter table table1 add id int unsign ...
- C# JS URL 中文传参出现乱码的解决方法
在传参是先编码在传输,接受时先编码,在接收. string mm=Server.URLEncode(你); Response.Redirect(index.aspx?mm=+mm); 然后在接收页解码 ...