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. 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 (≤). 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
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int T;
cin>>T;int sum=,tmp,max=-;
vector<int> vec;
vector<int> res;
vector<int> all;
while(T--){
cin>>tmp;
all.push_back(tmp);
vec.push_back(tmp);
sum+=tmp;
if(sum>max) {
max=sum;
res=vec;
}
if(sum<){
sum=;
vec.clear();
}
}
if(max!=-) cout<<max<<" "<<res[]<<" "<<res[res.size()-];
else cout<<<<" "<<all[]<<" "<<all[all.size()-];
system("pause");
return ;
}

浙大数据结构课后习题 练习一 7-1 Maximum Subsequence Sum (25 分)的更多相关文章

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

  2. 数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  3. 浙大数据结构课后习题 练习三 7-4 List Leaves (25 分)

    Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...

  4. 浙大数据结构课后习题 练习二 7-3 Pop Sequence (25 分)

    Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and p ...

  5. 浙大数据结构课后习题 练习二 7-2 Reversing Linked List (25 分)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  6. 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

  7. PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分)

    PTA数据结构与算法题目集(中文)  7-37 模拟EXCEL排序 (25 分) 7-37 模拟EXCEL排序 (25 分)   Excel可以对一组纪录按任意指定列排序.现请编写程序实现类似功能. ...

  8. PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分)

    PTA数据结构与算法题目集(中文)  7-35 城市间紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市 ...

  9. 网易云数据结构- Maximum Subsequence Sum

    题目 题目地址 思路 显然是最大子列和的进化版,那就先思考下经典的最大子列和.这也是道思维题,啥算法也没用到,全是思维技巧,真心不知道考试遇到这种题该怎么办了. 存放答案的一个类,我把它看成一个袋子, ...

随机推荐

  1. 学习使用Jmeter做压力测试(一)--压力测试基本概念

    学习使用Jmeter做压力测试(一)--压力测试基本概念 一.性能测试的概念 性能测试是通过自动化的测试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测 ...

  2. kill-9 kill-15

    kill -9 PID 是操作系统从内核级别强制杀死一个进程. kill -15 PID 可以理解为操作系统发送一个通知告诉应用主动关闭. kill -15 PID 效果是正常退出进程,退出前可以被阻 ...

  3. localStorage基本了解及使用

    以下内容来自: https://www.cnblogs.com/st-leslie/p/5617130.html  感谢大佬的分享 一.什么是localStorage.sessionStorage 在 ...

  4. Sqlserver实现故障转移 — AlwaysOn实现故障转移(4)

    目的:在已经加域的计算机上安装sqlserver2012,并配置系统级故障转移及数据库,实现AlwayOn. 域控的建立详见:https://www.cnblogs.com/xiaoerlang90/ ...

  5. 【学习笔记】APP页面,随机选择一个选项

    如下页面,一个测评功能,30个题目,每题的答案选项一样: 要实现每题自动随机选择一个答案 方法一:定义一个5个选项的列表,循环30次,然后使用random.shuffle打乱列表顺序,根据列表元素定位 ...

  6. 小程序入门 MQTT物联网协议 publish 和订阅subscribe onenet 阿里云 百度云 基于GPRS模块(SIM800C/SIM900A/SIM868等)和STM32主控芯片

    本文基本公开了如何移植MQTT物联网协议到STM32平台上,并结合GPRS模块(SIM800C/SIM900A/SIM868等)实现publish和订阅topic从onenet,阿里云,百度云等.如果 ...

  7. BeanFactory 和FactoryBean的区别

    转自:https://www.cnblogs.com/aspirant/p/9082858.html BeanFacotry是spring中比较原始的Factory.如XMLBeanFactory就是 ...

  8. EM算法分析

    参考来源: <机器学习>——周志华 https://www.cnblogs.com/jerrylead/archive/2011/04/06/2006936.html 几个概念 极大似然估 ...

  9. WINDOWS mysql 5.7.15 安装配置方法图文教程

    因本人需要需要安装Mysql,现将安装过程记录如下,在自己记录的同时,希望对有疑问的人有所帮助. 一.下载软件 1. 进入mysql官网,登陆自己的oracle账号(没有账号的自己注册一个),下载My ...

  10. 安装破解MyEclipse2017CI

    一.下载 1.Myeclipse官网下载地址:http://www.myeclipsecn.com/download/ 2.破解补丁链接:https://pan.baidu.com/s/1Ge_fbm ...