浙大数据结构课后习题 练习一 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
题目 题目地址 思路 显然是最大子列和的进化版,那就先思考下经典的最大子列和.这也是道思维题,啥算法也没用到,全是思维技巧,真心不知道考试遇到这种题该怎么办了. 存放答案的一个类,我把它看成一个袋子, ...
随机推荐
- koa-session 持久化
一.使用mongoose链接数据库 'use strict'; const mongoose = require('mongoose'); const config = require('config ...
- Linux删除命令rm
在用Linux的时候,有时分要删除一个文件夹,常常会提示次此文件非空,没法删除,这个时分,必需运用rm -rf命令.关于一些小白用户常常在运用Linux命令,会十分当心,以免搞出一些事情,下面小编将教 ...
- 电脑的sid
SID的查询方法:1.Win键+R键,打开运行,输入CMD2.输入:whoami /user3.就可以看到本机的SID了 SID的修改方法1.下载NewSID软件,并打开 2.可以指定一个SID,也可 ...
- 【DSP开发】解读TI的KeyStone II云技术应用
最近,德州仪器(TI)公司推出6款最新KeyStone II多核SoC,助力云应用.TI公司多核DSP中国市场开发经理蒋亚坚先生向媒体讲解了这6款KeyStone II新产品的特点与目标应用. ...
- C++学习笔记-C++对C语言的扩充和增强
C++兼容C,在C的基础上学习C++是一个不错的选择,也能够更好的了解C与C++的区别与联系. 变量定义 C语言中的变量都必须在作用域开始的位置定义 C++中更强调语言的实用性,所有的变量都可以在需要 ...
- 解决Wamp的 Error D:\wamp or PHP path 错误
之前早早就用了wamp,发现还是挺好用的,就是刚开始改端口号之类的配置有点麻烦,不过还是一一解决了. 就在昨天安装了 composer . 突然发现wamp 有一个错 “Error D:\wamp o ...
- 分享个昨天学的,sqlserver查表的所有列的列名,类型,长度的sql
select a.name as 列名, a.length as 长度,b.name as 类型 from syscolumns a left join systypes b on a.xtype ...
- 西安邀请赛-D(带权并查集+背包)
题目链接:https://nanti.jisuanke.com/t/39271 题意:给定n个物品,m组限制,每个物品有个伤害值,现在让两个人取完所有物品,要使得两个人取得物品伤害值之和最接近,输出伤 ...
- myeclipse显示db-brower
myeclipse显示db-brower 这东西怎么调出来? windows->show view->other->db borwser
- 小白学习django第六站-http相关
请求与相应 HttpRequest对象API def home(request): print('path:', request.path) print('mothod:', request.meth ...