中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 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 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分)的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2016秋期末考试
判断题: 1-1 N2logN和NlogN2具有相同的增长速度. (2分) 1-2 对一棵平衡二叉树,所有非叶结点的平衡因子都是0,当且仅当该树是完全二叉树.(2分) 1-3 无向连通图所有顶点的度之 ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2016秋期中考试
判断题: 1-1 算法分析的两个主要方面是时间复杂度和空间复杂度的分析. (2分) 1-2 将N个数据按照从小到大顺序组织存放在一个单向链表中.如果采用二分查找,那么查找的平均时间复杂度是O(logN ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2017春
中国大学MOOC-陈越.何钦铭-数据结构-2017春 学习地址 详细学习内容 Github记录地址 欢迎fork和star,有惊喜值得学习! 参考学习笔记 参考AC代码 数据结构和算法学习笔记 学习内 ...
- 中国大学MOOC-陈越、何钦铭-数据结构-笔记
中国大学MOOC-陈越.何钦铭-数据结构-2017春 跟着<中国大学MOOC-陈越.何钦铭-数据结构-2017春>学习,平时练习一下pat上的作业外:在这里记录一下:平时学习视屏的收获. ...
- 中国大学MOOC中的后台文件传输
早期版本的中国大学MOOC一旦被挂起后,应用在完成当前下载任务后无法继续添加新任务,当然也无法将缓存状态写入数据库.这个问题能否顺利解决直接关系到用户体验. 顺便吐槽下,凡是使用了后台文件传输还提示你 ...
- 中国大学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 ...
- 中国大学MOOC课程信息之数据分析可视化二
版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/82318571 - 写在前面 本篇博客继续对中国大学MOOC ...
- 中国大学MOOC课程信息之数据分析可视化一
版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/82263391 9月2日更:中国大学MOOC课程信息之数据分 ...
- 中国大学MOOC课程信息爬取与数据存储
版权声明:本文为博主原创文章,转载 请注明出处: https://blog.csdn.net/sc2079/article/details/82016583 10月18日更:MOOC课程信息D3.js ...
随机推荐
- vue 基础-->进阶 教程(1): 基础(数据绑定)
第一章 建议学习时间4小时 课程共3章 前面的nodejs教程并没有停止更新,因为node项目需要用vue来实现界面部分,所以先插入一个vue教程,以免不会的同学不能很好的完成项目. 本教程,将从零 ...
- Struts2教程
一.初识Struts2 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的 ...
- 很好的复习资料: SQL语句到底怎么写 ?
本文用到的数据库如下: CREATE DATABASE exam; /*创建部门表*/ CREATE TABLE dept( deptno INT PRIMARY KEY, dname VARCHAR ...
- 分享几个 git 的使用场景
你真的会使用 git 吗?你能回答下面几个问题吗? 有三个commit(顺序:CommitA.CommitB.CommitC),它们相互独立,没有依赖. 在不修改B.C的前提下,修改A,怎么操作? 合 ...
- DOCKER 从入门到放弃(二)
搜索镜像 从docker官方镜像仓库搜索镜像 docker search [OPTIONS] TERM OPTIONS: --automated :只显示自动创建的镜像,默认值为fasle --fil ...
- Azure MySQL PaaS (3) 创建MySQL异地只读数据库 (Master-Slave)
<Windows Azure Platform 系列文章目录> Azure MySQL PaaS服务提供异地只读的功能,我们可以在主站点,比如Azure上海数据中心,创建MySQL主节点. ...
- 一级缓存二级缓存(hibernate)
缓存是介于应用程序和物理数据源之间,其作用是为了降低应用程序对物理数据源访问的频次,从而提高了应用的运行性能.缓存内的数据是对物理数据源中的数据的复制,应用程序在运行时从缓存读写数据,在特定的时刻或事 ...
- mysql性能监控工具
参考文档: http://www.linuxidc.com/Linux/2012-09/70459.htm 1.记录慢查询SQL #配置开启 (linux)修改my.cnf: log-slow-que ...
- linux 配置Apache 、PHP
1. 安装 Apache 安装apache,首先要使用管理员权限,如果如法获取请联系您的管理员. centos: yum install httpd httpd-devel 2. 安装PHP 同样也需 ...
- voa 2015 / 4 / 18
Words in This Story gerund - n. an English noun formed from a verb by adding -ing infinitive - n. th ...