PAT 解题报告 1007. 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:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
10 1 4
题目描述:
求最大连续子段和并且输出该最大字段和序列的第一个和最后一个元素
算法分析:
思路1、暴力搜索
O(N2)的时间复杂度,K<10000,不超时
int sum = , mx = -INF, s, t;
for (int i=; i<K; i++) {
sum = ;
for (int j=i; j<K; j++) {
sum += n[j];
if (sum > mx) {
mx = sum;
s = i;t = j;
}
}
}
printf("%d %d %d", mx,n[s], n[t]);
思路2、扫描
经典DP问题, 基于这样一个事实:保存一个最大字段和以及一个当前子段和, 如果当前字段和大于当前最大字段和, 那么更新这个最大字段和, 如果当前字段和为负数的时候, 直接把当前字段和甚设置成0, 求最大字段和算法如下:
int MaxSum(int A[], int N) {
int currentSum = ;
int maxSum = ;
for(int i = ; i < N; ++i) {
currentSum += A[i];
if(currentSum > maxSum) maxSum = currentSum;
else if(currentSum < ) currentSum = ;
}
return maxSum;
}
由于这个题目里面还需要保存最大子段和的第一个和第二个元素, 加一些额外的变量记录以及在对应的更新maxSum和把currentSum设置成0的时候对应进行维护就行了. 算法复杂度O(N)
注意点:
当序列中有0但是其他都是负数的时候, 不是输出真个序列的第一个和最后一个元素,而是输出第一个0. 比如3 -1 0 -1 应当输出 0 0 0
PAT 解题报告 1007. Maximum Subsequence Sum (25)的更多相关文章
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 经典题
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)
简单DP. 注意:If all the K numbers are negative, then its maximum sum is defined to be 0, and you are sup ...
- 【PAT甲级】1007 Maximum Subsequence Sum (25 分)
题意: 给出一个整数K(K<=10000),输入K个整数.输出最大区间和,空格,区间起点的数值,空格,区间终点的数值.如果有相同的最大区间和,输出靠前的.如果K个数全部为负,最大区间和输出0,区 ...
- PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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 ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PAT 1007 Maximum Subsequence Sum (25分)
题目 Given a sequence of K integers { N1 , N2 , ..., NK }. A continuous subsequence is define ...
随机推荐
- what is php?
PHP is a popular general-purpose scripting language that is especially suited to web development. Fa ...
- session配置理解
session.cache_limiter 指定会话页面所使用的缓冲控制方法,默认为nocache.session.cache_expire 以分钟数指定缓冲的会话页面的存活期,默认为180.此设定对 ...
- hbase基本命令
基本命令 建表scores 具有两个列族grad 和courese create 'scores','grade', 'course' 查看当前HBase中具有哪些表 list 查看表结构 des ...
- 设置MyEclipse开发项目时使用的JDK
安装好MyEclipse之后,在MyEclipse中开发项目时,默认使用的是MyEclipse是自带的JDK,如下图所示: 如果我们需要使用自己安装好的JDK,那么就需要在MyEclipse中重新设置 ...
- LINQ延迟查询的例子
//linq延迟查询.两次查询结果不同 List<string> l = new List<string>() { "aaa", "bbb&quo ...
- VB的注释
VB程序中的注解有两种: 第1种是以单引号 ' 开头的,但如果这个符号是在双引号之内的,则视为文本,不做为注释引导符,这个符号后面的内容均为注释内容. 第2种就REM后加注释内容(REM与注释内容要空 ...
- C#中String 和string 的区别
C#中同时存在String与string MSDN中对string的说明: string is an alias for String in the .NET Framework.string是Str ...
- Top Five Communication Skills for Project Managers
Research among project managers globally identifies top communication skills for leading teams. Lead ...
- echarts -01 入门
1.效果图 2. code <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- html5头部说明
<!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --><html lang="zh-cmn-Hans"> ...