问题: 给定一整数序列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)的更多相关文章

  1. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

  2. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  3. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  4. [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 ...

  5. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  6. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  7. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  8. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  9. [Data Structure] LCSs——最长公共子序列和最长公共子串

    1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...

  10. 51nod1134(最长递增子序列)

    题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1134 题意: 中文题诶~ 思路: 直接暴力的话时间复杂度为 ...

随机推荐

  1. D6 I

    I - I Time Limit:1000MS     Memory Limit:2048KB     64bit IO Format:%lld & %llu Submit Status Pr ...

  2. 字符串截取函数substr和substring的不同及其相关说明

    1.substr 方法 功能:用于返回一个从指定位置开始的指定长度的子字符串,从“母字符串”的“指定位置”开始提取“指定长度”的“子字符串”. 语法:stringObject.substr(start ...

  3. HashMap两种遍历数据的方式

    HashMap的遍历有两种方式,一种是entrySet的方式,另外一种是keySet的方式. 第一种利用entrySet的方式: Map map = new HashMap(); Iterator i ...

  4. 2016 Multi-University Training Contest 7

    6/12 2016 Multi-University Training Contest 7 期望 B Balls and Boxes(BH) 题意: n个球放到m个盒子里,xi表示第i个盒子里的球的数 ...

  5. Float Equal Problem

    Understand limitations of floating point representations.Never check for equality with ==. Instead, ...

  6. 动态树之link-cut tree

    说好的专题... lct的一些概念看论文 杨哲<QTREE解法的一些研究> 简单易懂. 首先不要把lct想象得很难,其实很水的.lct就是很多splay树维护的树... lct的acces ...

  7. USACO 5.4 Telecowmunication(最大流+枚举)

    面对最小割之类的题目,完全木想法... 枚举+最大流..复杂度很大了...居然很快的就过了.. /* ID: cuizhe LANG: C++ TASK: telecow */ #include &l ...

  8. Java实现FTP上传下载功能

    Java FTP客户端工具包很多,在此我选用的Apache的FTPClient.这个包的获取可以通过http://commons.apache.org/net/来获取,我使用的是最新的commons- ...

  9. [LintCode] Cosine Similarity 余弦公式

    Cosine similarity is a measure of similarity between two vectors of an inner product space that meas ...

  10. 9.0 alpha 版安装出现 could not execute command lessc 的问题

    解决方案: apt-get install node-less