浙大数据结构课后习题 练习一 7-1 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. 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 分)的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- 数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 浙大数据结构课后习题 练习三 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 ...
- 浙大数据结构课后习题 练习二 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 ...
- 浙大数据结构课后习题 练习二 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 ...
- 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分)
PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分) 7-37 模拟EXCEL排序 (25 分) Excel可以对一组纪录按任意指定列排序.现请编写程序实现类似功能. ...
- PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分)
PTA数据结构与算法题目集(中文) 7-35 城市间紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市 ...
- 网易云数据结构- Maximum Subsequence Sum
题目 题目地址 思路 显然是最大子列和的进化版,那就先思考下经典的最大子列和.这也是道思维题,啥算法也没用到,全是思维技巧,真心不知道考试遇到这种题该怎么办了. 存放答案的一个类,我把它看成一个袋子, ...
随机推荐
- PTA --- Basic Level 1009 说反话
1009 说反话 (20 point(s)) 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式: 测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串.字符串由 ...
- fiddler的使用:抓包定位、模拟弱网
一.fiddler抓包定位 Fiddler是一个http协议调试代理工具,它能够记录并检查所有你的电脑和互联网之间的http通讯,设置断点,查看所有的“进出”Fiddler的数据(cookie,htm ...
- 【HANA系列】【第五篇】SAP HANA XS的JavaScript API详解
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列][第五篇]SAP HANA XS ...
- Xing: The Land Beyond — 从课堂到 Steam* 的卓越之旅
Xing:The Land Beyond 的诞生最初源于大学的一个关卡设计课程,之后才登录 Kickstarter* 平台,采用虚拟现实技术,并由 Sony* 带到电子娱乐展览会.这个设计任务本来计划 ...
- 【AMAD】tenacity -- Python中一个专门用来retry的库
动机 简介 用法 基本用法 何时停止 尝试间的等待 何时retry 其它 热度分析 源码分析 个人评分 动机 很多时候,我们都喜欢为代码加入retry功能.比如oauth验证,有时候网络不太灵,我们希 ...
- 华为HCNA乱学Round 11:PPPOE
- DatePickerDialog与OnDateSetListener基本用法与常见问题
日期时再显示更改控件一般我们使用构造方法public DatePickerDialog(@NonNull Context context, @Nullable OnDateSetListener li ...
- (转)Linux中显示空闲内存空间的free命令的基本用法
这篇文章主要介绍了Linux系统中free命令的基本用法,用free命令查看内存空余信息是Linux系统入门学习中的基础知识,需要的朋友可以参考下 free 命令显示系统使用和空闲的内存情况,包括 ...
- JAVA第四周总结
Java实验报告二 第一题 写一个名为Rectangle的类表示矩形.其属性包括宽width.高height和颜色color,width和height都是double型的,而color则是String ...
- LCA模板(数剖实现)
题目链接:https://www.luogu.org/problemnew/show/P3379 题意:LCA模板题. 思路:今天开始学树剖,先拿lca练练.树剖解lca,两次dfs复杂度均为O(n) ...