1007. Maximum Subsequence Sum (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

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

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

  2. pat1007. Maximum Subsequence Sum (25)

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

  3. Algorithm for Maximum Subsequence Sum z

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

  4. 中国大学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​​ }. ...

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

  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. Unable To Import Or Enter Sale Order - ORA-20001: APP-FND-01564: ORACLE error - 1422 in get_seq_info

    In this Document   Symptoms   Cause   Solution APPLIES TO: Oracle Order Management - Version 12.0.4 ...

  2. Matlab以MEX方式调用C源代码

    #include "mex.h" // 使用MEX文件必须包含的头文件 // 执行具体工作的C函数 double add(double x, double y) { return ...

  3. Linux多线程编程初探

    Linux线程介绍 进程与线程 典型的UNIX/Linux进程可以看成只有一个控制线程:一个进程在同一时刻只做一件事情.有了多个控制线程后,在程序设计时可以把进程设计成在同一时刻做不止一件事,每个线程 ...

  4. DEVICE_ATTR实例分析

    在内核中, sysfs 属性一般是由 __ATTR 系列的宏来声明的,如对设备的使用 DEVICE_ATTR ,对总线使用 BUS_ATTR ,对驱动使用 DRIVER_ATTR ,对类别(class ...

  5. Git与远程reposiory的相关命令

    问题1:Git如何同步远程repository的分支(branch) 某天,小C同学问我,为啥VV.git仓库里面本来已经删除了branchA这个分支,但是我的mirror中还是有这个分支呢? 分析: ...

  6. Swift之GCD 使用指南2

    Grand Central Dispatch大中枢派发:joy: 或俗称 GCD 是一件极其强大的武器.它为你提供了很多底层工具(比如队列和信号量),你可以组合这些工具来实现自己想要的多线程效果.不幸 ...

  7. 使用钩子参与到TCP拥塞事件的处理中

    TCP定义了几个拥塞事件,当这些事件发生时,我们可以通过TCP的拥塞控制算法,调用自定义的处理函数, 来做一些额外的事情的.也就是说,我们可以很简便的参与到TCP对拥塞事件的处理过程中. Author ...

  8. 飞鱼相册笔记(1)----外置SD卡文件夹名称不区分大小写

    飞鱼相册笔记(1)----外置SD卡文件夹名称不区分大小写 在飞鱼相册发布的第一个测试版中,很多用户表示无法查看外置SD卡中的照片.乍一听觉得加个外置SD卡的根目录,然后在扫描所有图片的时候把这个根目 ...

  9. phantomjs 爬去动态页面

    最近有一个小需求,需要根据用户输入的某宝的店铺 url,检查地址是否存在,并抓取店铺名称.某宝店铺 url 的 title 通常是 xx-xx-xx 的形式,中间的 xx 就是对应的店铺名称. 这个需 ...

  10. ios中block访问外部变量的一些注意点

    Block类型是一个C级别的语法和运行机制.它与标准的C函数类似,不同之处在于,它除了有可执行代码以外,它还包含了与堆.栈内存绑定的变量.因此,Block对象包含着一组状态数据,这些数据在程序执行时用 ...