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. phpstorm显示行号

    在Windows上. 其View->Active Editor->Show Line Numbers (仅适用于当前和变化File->Settings->Editor-> ...

  2. 【LeetCode】89. Gray Code

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

  3. 如何使用apktool反编译,查看androidmanifest的内容

    1.下载apktool http://pan.baidu.com/s/1o7Jy090 2.使用方法

  4. 手机端 图片的移动缩放旋转兼容touch

    //缩放var initialScale = 1;var currentScale = 1;touch.on('#target', 'pinch', function (ev) { currentSc ...

  5. pouchdb-find( pouchdb查询扩展插件 ,便于查询)

    pouchdb-find pouchdb-find 环境搭建 下载lib bower install pouchdb-find 引入js <script src="pouchdb.js ...

  6. Flex Robotlegs

    Flex Robotlegs 一.基于Robotlegs框架 flex应用基本组成 ProjectNameContext.as 用于配置 Robotlegs 的映射 ProjectName.mxml ...

  7. c# webbrower 代理 类 IEProxy

    using System;using System.Collections.Generic;using System.Linq;using System.Runtime.InteropServices ...

  8. 关于一点jeesite

    最近刚接触jeesite 深深被这个功能强大的框架所折服,虽然其中有一些地方还不完善,但也不妨碍我们通过jeesite提高我们java水平的大门. 这两天在网上一直在找关于jeesite的文章,看来看 ...

  9. Android端恶意锁屏勒索应用分析

    一.前言 5月12日,一场全球性互联网灾难悄然而至,一款名为WannaCRY的PC端恶意勒索软件利用NSA泄漏的危险漏洞“永恒之蓝”,给100多个国家和地区10万台电脑造成了巨大的损失.到2017年为 ...

  10. God 1.1.1 多线程之内存可见性

    共享变量在线程间的可见性 synchronize实现可见性 volatile实现可见性 指令重排序 as-if-serial语义 volatile使用注意事项 synchronized和volatil ...