Maximum Subsequence Sum(接上篇)
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: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.
2.The numbers must be separated by one space, but there must be no extra space at the end of a line.
3.最后一点没提到,也就是我一直不能AC的原因:
当sequence中 全是非正数的情况,并不能归纳到 1 中,举个例子:sequence : -1 -2 -3 0 -5
out:0 0 0 而不是 0 -1 -5
code:
#include <iostream>
#include <vector>
using namespace std; int SubSum(vector<int>& v,int& m,int& n,bool& find){
int MaxSum = ,tmp = ,tmp_m = v[];//先移动tmp_m
m = n = v[];//当sequence比当前大再把m移到tmp_m处
for(int i=;i<v.size();++i){
tmp += v[i];
if(tmp > MaxSum){
find = true;
int tmp_max = MaxSum;//tm_max用来判断是否移动n
MaxSum = tmp;
m =tmp_m;
if(MaxSum > tmp_max)
n = v[i];
}
else if(tmp < ){
tmp = ;
tmp_m = v[i+];
}
}
int max = v[];
for(int i=;i<v.size();++i)
if(v[i]>max){
max =v[i];
find = true;
}
if(max == )
m = n =;
return MaxSum;
} int main()
{
int n,tmp,start,end;
bool find =false;//是否存在最大子队列
vector<int> v;
cin >> n;
for(int i=;i<n;++i){
cin >> tmp;
v.push_back(tmp);
}
n = SubSum(v,start,end,find);
if(find)
cout << n << ' ' << start << ' ' << end << endl;
else
cout << n << ' ' << v.front() << ' ' << v.back() << endl;
return ;
}
Maximum Subsequence Sum(接上篇)的更多相关文章
- 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 }. ...
- PAT1007:Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- 【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 ...
- 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 ...
随机推荐
- C#引用C++代码
现在在Windows下的应用程序开发,VS.Net占据了绝大多数的份额.因此很多以前搞VC++开发的人都转向用更强大的VS.Net.在这种情况下,有很多开发人员就面临了如何在C#中使用C++开发好的类 ...
- BI之SSAS完整实战教程5 -- 详解多维数据集结构
之前简单介绍过多维数据集(Cube)的结构. 原来计划将Cube结构这部分内容打散,在实验中穿插讲解, 考虑到结构之间不同的部分都有联系,如果打散了将反而不好理解,还是直接一次性全部讲完. 本篇我们将 ...
- 关于下载DynamicDataDisplay.dll后被默认锁定的问题
问题:命名空间 d3:“http://research.microsoft.com/DynamicDataDisplay/1.0”不存在ChartPlotter元素 原因:下载DynamicDataD ...
- 360 webscan中防注入跨站攻击的核心
//get拦截规则 $getfilter = "\\<.+javascript:window\\[.{1}\\\\x|<.*=(&#\\d+?;?)+?>|< ...
- FreeBSD 10 发布
发行注记:http://www.freebsd.org/releases/10.0R/relnotes.html 下文翻译中... 主要有安全问题修复.新的驱动与硬件支持.新的命名/选项.主要bug修 ...
- 【JVM】2、关于jdk7的MethodHandle类
关于MethodHandle类,这个类是在jdk1.7之后加入的,这个类的作用类似函数指针的意思 这个类中有一个方法 这里我的jdk有一个问题,就是我在进行MethodHandle操作的时候,我们会发 ...
- Java中的继承
我们在以前的学习中,我们会了C#中的继承,今天我们来了解了解Java中的继承,其实都大同小异啦! 1.语法 修饰符 SubClass extends SuperClass(){ //类定义部分 } e ...
- mysql乱码以及Data too long for column全解(最完整实用版)
今天系统升级,开发.测试说本地环境.测试环境都没有问题,都用ssh client升的,演示环境报错了Data too long for column. 仔细检查了下,表字符集都是utf-8,目测长度肯 ...
- 如何查询拥有执行某个Tcode权限所有人员
方法很简单,如下 一:Tcode:S_BCE_68001400二:输入你想查询的Tcode,例如:SE38 打开如下图所示,然后执行即可 三:AUTH(关于权限的控制),打开如下图所示.上图“ ...
- SQL Server 分组 去除从复列
下面先来看看例子: table表 字段1 字段2 id name 1 a 2 b 3 c 4 c 5 ...