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 ...
随机推荐
- php 使用GD库上传图片以及创建缩略图
php 使用GD库上传图片以及创建缩略图 GD库是PHP进行图象操作一个很强大的库. 先在php.ini里增加一行引用:extension=php_gd2.dll 重启apache.做一个测试页 ...
- 读取C#AssemblyInfo文件中的AssemblyVersion中的值
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); 1.程序集的版本信息由下面四个值组成:主 ...
- Nginx Location 语法,与简单配置[转]
一、介绍Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器 ...
- Machine Learning in Action -- Logistic regression
这个系列,重点关注如何实现,至于算法基础,参考Andrew的公开课 相较于线性回归,logistic回归更适合用于分类 因为他使用Sigmoid函数,因为分类的取值是0,1 对于分类,最完美和自然的函 ...
- opencv提取截获图像(总结摘来)
opencv提取截获图像(总结摘来) http://blog.csdn.net/wuxiaoyao12/article/details/7305865 版权声明:本文为博主原创文章,未经博主允许不得转 ...
- 使用第三方分页AspNetPager实现真正分页的SQL原理
AspNetPager是一个第三方分页第三方控件,可以和数据绑定控件(GridView等)方便的结合,实现真分页. 真分页:从数据库中获取符合要求的部分数目的记录.性能较高,数据量小,网络负载小,对数 ...
- UI---startup--jquery
http://www.w3school.com.cn 传统的基于表单提交, 整页刷新式的并不需要前端MVC. 当 然这种体验会很糟糕.试想一下, 用WebQQ时,每发一次消息页面就要泛白一次, 这是什 ...
- 介绍UDF,以及完成大小写的转换
一:概述 1.UDF 用户自定义函数,用java实现自定义的需求 2.UDF的类型 udf:一进一出 udaf:多进一出 udtf:一进多出 3.udf的实现步骤 继承UDF类 实现evaluate的 ...
- 远程通知APNs(Apple Push Notification Server)
推送通知是由应用服务提供商发起的,通过苹果的APNs(Apple Push Notification Server)发送到应用客户端.下面是苹果官方关于推送通知的过程示意图: 推送通知的过程可以分为以 ...
- Linux 下动态库 / 静态库(依赖)
一. 依赖动态库的动态库 libfun.so依赖动态库libtest.so(libfun.so动态库里的函数intnothing()调用了libtest.so里的intmytest()函数),而mai ...