01-复杂度2 Maximum Subsequence Sum   (25分)

Given a sequence of K
integers { N​1​​,N​2​​,
..., N​K​​
}. A continuous subsequence is defined to be { N​i​​,N​i+1​​,
..., N​j​​
} 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 integerK
(≤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
  • 时间限制:200ms
  • 内存限制:64MB
  • 代码长度限制:16kB
  • 判题程序:系统默认
  • 作者:陈越
  • 单位:浙江大学

大致题意:

此题为 最大子列和 的变式,找出序列中的最大子序列之和,并输出子序列中初始位的数和末位的数

解题思路:

用在线处理的方式,用thisMax储存当前临时序列和,如果大于最大maxSum的值,则更新maxSum的值

如果thisSum的值小于0时,将thisSum的值赋为0,因为thisSum小于0时,再加上一个数肯定比之前还要小

具体解答请看以下代码:

#include <iostream>
/*
* author:Fayne
* time:2015-9-2 21:24:16
*thisSum用于保存临时序列之和,maxSum更新最大序列和
*left, right分别表示最大序列的左右序号,tempLeft保存临时左端的序号
*/
using namespace std;
int A[10010]; int main()
{
int k, i;
cin >> k;
for ( i=0; i<k; i++ )
cin >> A[i];
int left = 0, right = k-1, maxSum = -1, thisSum = 0, tempLeft;//maxSum赋初值为-1为了解决出现全部序列为负的情况
for ( i=0; i<k; i++ )
{
thisSum += A[i]; if ( thisSum > maxSum )//如果临时序列和大于最大和,则更新最大和
{
maxSum = thisSum;
left = tempLeft;//将临时左端的序号赋值给左端序号
right = i;
}
else if ( thisSum < 0 )//thisSum小于0时,从此刻下一个开始重新求和
{
thisSum = 0;
tempLeft = i+1;//把此刻的下一序号赋值给临时左端序号
}
}
if ( maxSum < 0 )//maxSum < 0 说明整个序列全为负数,根据题意,最大和应该为0
maxSum = 0;
cout << maxSum << " " << A[left]<< " " << A[right]<< endl; return 0;
}

注意题意:If all theK
numbers are negative, then its maximum sum is defined to be 0

中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)的更多相关文章

  1. 中国大学MOOC-陈越、何钦铭-数据结构-2016秋期末考试

    判断题: 1-1 N2logN和NlogN2具有相同的增长速度. (2分) 1-2 对一棵平衡二叉树,所有非叶结点的平衡因子都是0,当且仅当该树是完全二叉树.(2分) 1-3 无向连通图所有顶点的度之 ...

  2. 中国大学MOOC-陈越、何钦铭-数据结构-2016秋期中考试

    判断题: 1-1 算法分析的两个主要方面是时间复杂度和空间复杂度的分析. (2分) 1-2 将N个数据按照从小到大顺序组织存放在一个单向链表中.如果采用二分查找,那么查找的平均时间复杂度是O(logN ...

  3. 中国大学MOOC-陈越、何钦铭-数据结构-2017春

    中国大学MOOC-陈越.何钦铭-数据结构-2017春 学习地址 详细学习内容 Github记录地址 欢迎fork和star,有惊喜值得学习! 参考学习笔记 参考AC代码 数据结构和算法学习笔记 学习内 ...

  4. 中国大学MOOC-陈越、何钦铭-数据结构-笔记

    中国大学MOOC-陈越.何钦铭-数据结构-2017春 跟着<中国大学MOOC-陈越.何钦铭-数据结构-2017春>学习,平时练习一下pat上的作业外:在这里记录一下:平时学习视屏的收获. ...

  5. 中国大学MOOC中的后台文件传输

    早期版本的中国大学MOOC一旦被挂起后,应用在完成当前下载任务后无法继续添加新任务,当然也无法将缓存状态写入数据库.这个问题能否顺利解决直接关系到用户体验. 顺便吐槽下,凡是使用了后台文件传输还提示你 ...

  6. 中国大学mooc直播回放看这里哦http://www.icourse163.org/forum/1001974001/topic-1003372881.htm?sortType=1&pageIndex=1

    中国大学mooc直播回放看这里哦http://www.icourse163.org/forum/1001974001/topic-1003372881.htm?sortType=1&pageI ...

  7. 中国大学MOOC课程信息之数据分析可视化二

    版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/82318571 - 写在前面 本篇博客继续对中国大学MOOC ...

  8. 中国大学MOOC课程信息之数据分析可视化一

    版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/82263391 9月2日更:中国大学MOOC课程信息之数据分 ...

  9. 中国大学MOOC课程信息爬取与数据存储

    版权声明:本文为博主原创文章,转载 请注明出处: https://blog.csdn.net/sc2079/article/details/82016583 10月18日更:MOOC课程信息D3.js ...

随机推荐

  1. RDLC报表纵向合并单元格。

    在做RDLC报表时发现居然没有纵向合并单元格,震惊! 网上查了一些资料,有些方法很可爱,采用去除边框法,但是用这种方法如果要求文本属性居中的话那则达不到美观效果,还有些复杂一点的方法,我都没耐心看,然 ...

  2. Array和ArrayList的区别与联系

    博主今天去了一个java的实习面试,发现有好多java最基础的数据结构对于博主来说反而感到陌生,在面试官问一些常见的例如HashMap这样的数据结构,博主能回答的头头是道,但是在问到Array和Arr ...

  3. 3.ubuntu如何安装搜狗输入法

    1.http://blog.csdn.net/qq_21792169/article/details/53152700 2.http://jingyan.baidu.com/article/54b6b ...

  4. android studio gradle 两种更新方法更新

    android studio gradle 两种更新方法更新 第一种.Android studio更新 第一步:在你所在项目文件夹下:你项目根目录gradlewrappergradle-wrapper ...

  5. Pycharm直接连接Github

    Pycharm可以说是使用Python语言开发者的必备利器.高校学生有学生邮箱就可以免费使用,着实省了我不少银两.附个license图: Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很 ...

  6. Linux 安装依赖库

    ###安装依赖库###yum -y install rsync net-snmp syslog net-snmp-devel wget patch screen gcc gcc-c++ autocon ...

  7. redis内存管理

    Redis主要通过控制内存上线和回收策略来实现内存管理. 1. 设置内存上限 redis使用maxmemory参数限制最大可用内存.限制的目的主要有: 用户缓存场景,当超出内存上限maxmemory时 ...

  8. servlet+jsp导入Excel到mysql数据库

    package khservlet; import java.io.FileInputStream;import java.io.IOException;import java.io.InputStr ...

  9. poj3276

    Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5121   Accepted: 237 ...

  10. Chromium模块和进程模型

    i. Chromium基本模块 Chromium各模块层级图a) Chromium主要包括如下模块: WebKit:  Safari和Chromium,以及任何其他基于WebKit内核的浏览器所共享的 ...