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(接上篇)的更多相关文章

  1. Algorithm for Maximum Subsequence Sum z

    MSS(Array[],N)//Where N is the number of elements in array { sum=; //current sum max-sum=;//Maximum ...

  2. 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)

    01-复杂度2 Maximum Subsequence Sum   (25分) Given a sequence of K integers { N​1​​,N​2​​, ..., N​K​​ }. ...

  3. PAT1007:Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  4. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  5. 【DP-最大子串和】PAT1007. Maximum Subsequence Sum

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  6. PAT Maximum Subsequence Sum[最大子序列和,简单dp]

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  7. 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 ...

  8. 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 ...

  9. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

随机推荐

  1. MVC学习笔记索引帖

    [MVC学习笔记]1.项目结构搭建及单个类在各个层次中的实现 [MVC学习笔记]2.使用T4模板生成其他类的具体实现 [MVC学习笔记]3.使用Spring.Net应用IOC(依赖倒置) [MVC学习 ...

  2. C#-Windows服務以LocalSystem賬戶安裝的話無法獲取我的文檔路徑

    如圖,如果Window服務以上圖 Account安裝運行,則無法獲取到 以下路徑: System.Environment.GetFolderPath(Environment.SpecialFolder ...

  3. 一个让echarts中国地图包含省市轮廓的技巧

    背景知识及应用简介 本文主要介绍一个使用ECharts地图组件的取巧方法,该技巧源于实际需求中遇到的问题,一般没有该需求的话这个技巧也是用不到的.有前端基础和以及对ECharts有了解的人基本可以读懂 ...

  4. 数据库表被锁表,select会等待。

    SELECT * FROM dbo.Table_1 WITH(NOLOCK) 这样就可以不用等待,但数据读的是被锁之前的数据,表被锁了,肯定会有对表的update,delete操作. 如果对数据的准确 ...

  5. DP---Mahjong tree

    HDU  5379 Problem Description Little sun is an artist. Today he is playing mahjong alone. He suddenl ...

  6. Maven创建webapp(二)

    这一节将记录在myeclipse下用maven创建一个简单的webapp项目 web开发maven仓库自动添加组件,故需要需要保持网络的通畅. 打开myeclipse  -->  File  - ...

  7. jar包和war包的区别(转)

    jar包和war包的区别:war是一个web模块,其中需要包括WEB-INF,是可以直接运行的WEB模块.而jar一般只是包括一些class文件,在声明了Main_class之后是可以用java命令运 ...

  8. PowerBuilder反编译

            最近需要了解某个PowerBuilder程序如何工作的,这已经是某个时代的产物了.除了EXE之外,还有一些PBD文件.PBD文件是PowerBuilder动态库,作为本地DLL的一个替 ...

  9. [小北De编程手记] : Lesson 01 - Selenium For C# 之 环境搭建

    在我看来一个自动化测试平台的构建,是一种很好的了解开发语言,单元测试框架,自动化测试驱动,设计模式等等等的途径.因此,在下选择了自动化测试的这个话题来和大家分享一下本人关于软件开发和自动化测试的认识. ...

  10. Python数学运算的一个小算法(求一元二次方程的实根)

    请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程:ax² + bx + c = 0的两个解. #!/usr/bin/env python # -*- coding: ...