最大子序列和 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 题意: 中文题诶~ 思路: 直接暴力的话时间复杂度为 ...
随机推荐
- asp.net 未能写入输出文件--“拒绝访问”的解决办法
概述 将网部署在IIS 7 上,访问本地磁盘路径的时候,提示"**文件拒绝访问". 解决办法 将需要访问的文件夹赋于IIS_IUSRS完全控制的权限即可,不用添加NET ...
- ural 1437. Gasoline Station
1437. Gasoline Station Time limit: 1.0 secondMemory limit: 64 MB Once a gasoline meter broke at a fi ...
- ZOJ 3911 Prime Query ZOJ Monthly, October 2015 - I
Prime Query Time Limit: 1 Second Memory Limit: 196608 KB You are given a simple task. Given a s ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
- MySQL删除更新数据时报1175错误的问题
今天删除mysql数据库中的一条记录的时候,一直不能删除,提示错误信息如下: Error Code: 1175. You are using safe update mode and you trie ...
- javascript reverse string
var strReversed = str.split('').reverse().join(''); function: function reverse(str){ return str.spli ...
- access-Control-Allow-Origin跨域请求安全隐患
最新的W3C标准里是这么实现HTTP跨域请求的,Cross-Origin Resource Sharing,就是跨域的目标服务器要返回一系列的Headers,通过这些Headers来控制是否同意跨域. ...
- 【BZOJ】1048: [HAOI2007]分割矩阵
http://www.lydsy.com/JudgeOnline/problem.php?id=1048 题意:给出一个a×b(a,b<=10)的矩阵,带一个<=100的权值,现在要切割n ...
- 【BZOJ】2929: [Poi1999]洞穴攀行(最大流)
http://www.lydsy.com/JudgeOnline/problem.php?id=2929 题意描述不清..搞得我wa了一发.. 应该是,有1和n的点的边容量都为1,其余随便... 然后 ...
- include动作标记和include指令标记学习笔记
我的jsp学习参考书是耿祥义,张跃平编著的jsp大学使用教程这本书,我也向大家推荐这本书,我觉得这本书适合我的学习方式,知识的讲解透彻易懂. include指令标记 ...