PAT1007:Maximum Subsequence Sum
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 思路 DP的思想。
这道题最关键点在于:确定最大值子序列的左右索引left和right。那么:
1.从左到右遍历序列时,如果遍历到第i个数时的累加和已经小于0,那么说明从这个数左边开始的所有可能的左半部序列到这个数的和都已经小于0,对于这个数右半部分的序列只减不增,还不如舍弃掉。因此最大和子序列肯定只会在这个数右边。所以暂存下新的左索引templeft = i + 1。
2.当遍历到第i个数时当前最大值tempsum已经大于输出最大值maxsum时,maxsum = tempsum,那么需要更新左右索引left = templeft 和 right = i.
3.对于遍历后maxsum < 0,说明整个序列只可能全是负数,不存在最大正数和的情况,根据题目要求令maxsum = 0,左右索引即为元序列的左右索引。 代码
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int N;
while(cin >> N)
{
vector<int> nums(N);
int left = ,right = N - ,maxsum = -,tempsum = ,templeft = ;
for(int i = ;i < N;i++)
{
cin >> nums[i];
tempsum += nums[i];
if(tempsum < )
{
tempsum = ;
templeft = i + ;
}
else if (tempsum > maxsum)
{
left = templeft;
maxsum = tempsum;
right = i;
} }
if(maxsum < )
maxsum = ;
cout << maxsum << " " << nums[left] << " " << nums[right] << endl;
}
}
PAT1007:Maximum Subsequence Sum的更多相关文章
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- pat1007. Maximum Subsequence Sum (25)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- Algorithm for Maximum Subsequence Sum z
MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- PAT Maximum Subsequence Sum[最大子序列和,简单dp]
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- 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 ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
随机推荐
- gdb学习(二)[第二版]
查看运行时数据 print - 查看变量值 ptype – 查看变量类型 #ptype i #ptype "aaa" 打印字符串"aaa"的类型 #ptype ...
- SQL-Teradata基础
1.创建一个和表 pnr_1 结构一样的表 Create table pnr_2 as pnr_1 with no data 不含数据 Create table pnr_2 as pnr_1 wit ...
- Linux的常用命令(1) - 指定运行级别
命令:init [0123456] 运行级别 0:关机 1:单用户 2:多用户状态没有网络服务 3:多用户状态有网络服务 4:系统未使用保留给用户 5:图形界面 6:系统重启 常用运行级别是3和5,要 ...
- ubuntu下eclipse新建项目没有java project的解决办法
装好了eclipse之后却发现新建项目没有java project的选项,大致搜索了一下,并没有发现很好的解决方案(大都是让你重新安装什么的),于是开始瞎鼓捣,并且找到了一个方案: 在终端切换到roo ...
- MT6592 经验积累
1.build/target/product/xxxx.mk 新项目clone后,需要修改这里 如:build/target/product/x160v.mk PRODUCT_MODEL :=Phi ...
- Herriot
Herriot测试框架是Hadoop-0.21.0及以后版本中新加入的测试框架,它的出现主要是为了尽可能地模拟真实的大规模分布式系统,并且对该系统实现自动化测试.和Hadoop以前的测试框架MiniD ...
- 64位ubuntu安装N64模拟器mupen64
我们知道在windows平台下模拟器多如牛毛,N64的模拟器也不例外.而linux下对于想玩N64的童鞋们有啥好办法呢?我通过度娘找到一款开源的N64模拟器mupen64,其官方网址为:http:// ...
- C# 压缩PDF图片
文档中包含图片的话,会使得整个文档比较大,占用存储空间且不利于快速.高效的传输文件.针对一些包含大量高质图片的PDF文档,若是对图片进行压缩,可以有效减少文档的占用空间.并且,在文档传输过程中也可以减 ...
- css3-------:before和:after的作用
1.:before和:after的作用就是在指定的元素内容(而不是元素本身)之前或者之后插入一个包含content属性指定内容的行内元素,最基本的用法如下: <!doctype html> ...
- MySQL性能调优——索引详解与索引的优化
--索引优化,可以说是数据库相关优化.理解尤其是查询优化中最常用的优化手段之一.所以,只有深入索引的实现原理.存储方式.不同索引间区别,才能设计或使用最优的索引,最大幅度的提升查询效率! 一.BTre ...