Maximum Subsequence Sum(java)
7-1 Maximum Subsequence Sum(25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } 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)的更多相关文章
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT A1007 Maximum Subsequence Sum (25 分)——最大子列和,动态规划
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 第四周 Leetcode 124. Binary Tree Maximum Path Sum (HARD)
124. Binary Tree Maximum Path Sum 题意:给定一个二叉树,每个节点有一个权值,寻找任意一个路径,使得权值和最大,只需返回权值和. 思路:对于每一个节点 首先考虑以这个节 ...
- 01-复杂度2 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 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}, ...
- 1007 Maximum Subsequence Sum (PAT(Advance))
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 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 ...
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
随机推荐
- 【转】eclipse找不到或无法加载主类
Eclipse错误: 找不到或无法加载主类或项目无法编译10种解决大法! 今天启动项目,又遇到找不到或无法加载主类的情况,清除项目后无法编译,bin文件夹下没有.class文件,至少遇到3次这个问题了 ...
- 15期day01编程与计算机硬件
一.编程: 1,编程语言:定义:让计算机能像人一样去工作执行某种命令的语音 重点:工作的思维逻辑 编程语言为翻译 简单逻辑汉语 小例子: 接收用户输入的用户名 接收用户输入的密码 判断用户输入的用户名 ...
- jchdl-GSL-实例 - 使用Intellij IDEA创建Mux
https://mp.weixin.qq.com/s/yP9xKeg0iHJChuMPzxdJtA https://github.com/wjcdx/jchdl/blob/master/src/org ...
- Chisel3 - model - UserModule commands
https://mp.weixin.qq.com/s/0ECca6XyFyEri0B4ckOZ4A 介绍UserModule类中,如何管理构建硬件模型所需的命令. 1. _comma ...
- Java实现蓝桥杯VIP 算法训练 sign函数
问题描述 给定实数x,输出sign(x)的值. sign(x)是符号函数,如果x>0,则返回1:如果x=0,则返回0:如果x<0,则返回-1. 输入格式 一行一个实数x. 输出格式 一行一 ...
- html页面引用video.js播放m3u8格式视频
//head里面的内容,我是采用cdn引用的方式,因为项目太小 <head> <meta charset="utf-8" /> <title>二 ...
- 总结梳理:webpack中如何使用vue
1. 安装vue的包 cnpm i vue -S 2. 由于在webpack中,推荐使用 .vue这个组件模板文件定义的组件,所以,需要安装, 能解析这个文件的loader: cnpm i vu ...
- Alink漫谈(六) : TF-IDF算法的实现
Alink漫谈(六) : TF-IDF算法的实现 目录 Alink漫谈(六) : TF-IDF算法的实现 0x00 摘要 0x01 TF-IDF 1.1 原理 1.2 计算方法 0x02 Alink示 ...
- vs2019离线下载安装包
官方的离线安装说明-->点击打开 1.下载 vs2019引导程序 ,选择你所需的版本下载,我选择了企业版 vs_enterprise__184447765.1558180718.exe 2.设置 ...
- Crypto++ AES 加密解密流程
// aesdemo.cpp : 定义控制台应用程序的入口点. // #include <stdio.h>#include <tchar.h>#include <iost ...