7-1 Maximum Subsequence Sum(25 分)

Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:
Each input file contains one test case. Each case occupies two lines.The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

-     - -   -

Sample Output:

  

思路:最大子列和问题,输出和的同时,输出子列的起始项和结尾项。

通过在线处理法可知,获取最大子列和时,当前的arr[i]即为最大子列和的结尾项,那么我们只要记住最大子列的循环次数num(num即为最大子列的个数)

就可以通过数组的特性,即可获取最大子列的起始项 arr[i-num+1]。(最后+1的原因是,num为子列的个数,i-num后,索引在子列起始项的前一项,所以+1后为最大子列起始项位置所在)

代码如下:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer; public class Main { public static void main(String[] args) throws Exception {
//StreamTokenizer类获取屏幕输入,处理输入比Scanner类效率高
StreamTokenizer in = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in))); in.nextToken();
int n = (int) in.nval;
int[] num = new int[n];
for (int i = 0; i < n; i++) {
in.nextToken();
num[i] = (int) in.nval;
} //解决全是负数的情况
int count = 0;
for (int i = 0; i < n; i++) {
if (num[i] < 0) {
count++;
}
}
if (count == n) {
System.out.println(0 + " " + num[0] + " " + num[n - 1]);
} else { int[] b = maxSubseqSum(num);
System.out.print(b[0] + " " + b[1] + " " + b[2]);
}
} public static int[] maxSubseqSum(int[] arr) {
int thisSum = 0, maxSum = 0;
int start = 0, end = 0;
//记录循环次数,这样可以通过子列结尾值获取子列开头的值
int num = 0;
for (int i = 0; i < arr.length; i++) {
num++;
thisSum += arr[i];
if (thisSum > maxSum) {
maxSum = thisSum;
//当前和为最大值时,此时arr[i]为子列结尾值
end = arr[i];
//+1的原因:num为子列的个数,i-num后索引所在位置为子列起始位置的前一项。
start = arr[i - num + 1];
} else if (thisSum < 0) {
thisSum = 0;
num = 0;
}
}
return new int[]{maxSum, start, end};
}
}

Maximum Subsequence Sum(java)的更多相关文章

  1. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  2. PAT A1007 Maximum Subsequence Sum (25 分)——最大子列和,动态规划

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  3. 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)

    124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...

  4. 01-复杂度2 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  5. WUSTOJ 1338: The minimum square sum(Java)

    题目链接:1338: The minimum square sum Description Given a prime p(p<108), you are to find min{x2+y2}, ...

  6. 1007 Maximum Subsequence Sum (PAT(Advance))

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  7. PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  8. 【DP-最大子串和】PAT1007. Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  9. PAT Maximum Subsequence Sum[最大子序列和,简单dp]

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

随机推荐

  1. 自定义值类型一定不要忘了重写Equals,否则性能和空间双双堪忧

    一:背景 1. 讲故事 曾今在项目中发现有同事自定义结构体的时候,居然没有重写Equals方法,比如下面这段代码: static void Main(string[] args) { var list ...

  2. 使用Burpsuite对手机抓包的配置

    之前使用dSploit的时候就一直在想怎么对手机进行抓包分析,前两天使用了Burpsuite神器,发现通过简单的配置就可以抓手机app的数据包了,进而分析手机app的流量. 配置环境: 1.win7下 ...

  3. 04 . Mysql主从复制和读写分离

    Mysql AB复制 ​ AB复制又称之为主从复制,用于实现数据同步,实现Mysql的AB复制时,数据库的版本尽量保持一致,如果不能保持一致,最起码从服务器的版本要高于主服务器,但是就无法实现双向复制 ...

  4. Java实现 LeetCode 704 二分查找(二分法)

    704. 二分查找 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1 ...

  5. Java实现 LeetCode 52 N皇后 II

    52. N皇后 II n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击. 上图为 8 皇后问题的一种解法. 给定一个整数 n,返回 n 皇后不同的解决方案 ...

  6. java实现漏掉的账目明细

    某财务部门结账时发现总金额不对头.很可能是从明细上漏掉了某1笔或几笔.如果已知明细账目清单,能通过编程找到漏掉的是哪1笔或几笔吗? 如果有多种可能,则输出所有可能的情况. 我们规定:用户输入的第一行是 ...

  7. java实现纵横火柴棋

    [编程题] 这是一个纵横火柴棒游戏.如图[1.jpg],在一个3x4的方格中,游戏的双方轮流放置火柴棒.其规则是: 1. 不能放置在已经放置火柴棒的地方(即只能在空格中放置). 2. 火柴棒的方向只能 ...

  8. Python爬虫 爬取搜狗搜索到的内容页面

    废话不多说,直接上代码 import requests def main(): url='https://www.sogou.com/web' headers={ 'User_Agent':'Mozi ...

  9. Netty源码学习系列之1-netty的串行无锁化

    前言 最近趁着跟老东家提离职之后.到新公司报道之前的这段空闲时期,着力研究了一番netty框架,对其有了一些浅薄的认识,后续的几篇文章会以netty为主,将近期所学记录一二,也争取能帮未对netty有 ...

  10. (一)linux三剑客之grep

    给自己提出以下6个问题,看自己是否真正掌握了grep [1] grep 是什么? [2] grep 有什么作用 ? [4] grep 常用于何处 ? [5] grep 的基本用法 ? [6] grep ...