PAT Advanced 1007 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()
{
/**计算时间
* 开始7:14
* 结束7:27
*/
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 ;
}
PAT Advanced 1007 Maximum Subsequence Sum (25 分)的更多相关文章
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- PAT Advanced 1007 Maximum Subsequence Sum
题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...
- PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)
1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...
- PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT 1007 Maximum Subsequence Sum (25分)
题目 Given a sequence of K integers { N1 , N2 , ..., NK }. A continuous subsequence is define ...
- 数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 【PAT甲级】1007 Maximum Subsequence Sum (25 分)
题意: 给出一个整数K(K<=10000),输入K个整数.输出最大区间和,空格,区间起点的数值,空格,区间终点的数值.如果有相同的最大区间和,输出靠前的.如果K个数全部为负,最大区间和输出0,区 ...
- PAT 甲级 1007. Maximum Subsequence Sum (25) 【最大子串和】
题目链接 https://www.patest.cn/contests/pat-a-practise/1007 思路 最大子列和 就是 一直往后加 如果 sum < 0 就重置为 0 然后每次 ...
随机推荐
- 搭建Ambari 2.6.0 tar 解压缩报错
背景:我们使用的方式不是wget 去下载ambari的源码包,而是在windows 的 firefox 下直接下载,将文件存储到本地. 执行 tar -zxvf HDP-2.6.3.0-centos7 ...
- Future 和 CompletableFuture 异步任务 in Java
Future 异步任务 定义Callable接口的实现 import java.util.concurrent.Callable; public class RealDataCallable impl ...
- intellij idea中去除@Autowired注入对象的红色波浪线提示
idea中通过@Autowired注入的对象一直有下划线提示. 解决:改变@Autowired的检查级别即可. 快捷键:Ctrl+Alt+s,进入idea设置界面,输入inspections检索
- 解决VirtualBox虚拟机装XP无声问题的简便办法
原文地址;http://www.2cto.com/os/201206/134887.html 解决VirtualBox虚拟机装XP无声问题的简便办法 解决VirtualBox虚拟机装XP无声问题的简便 ...
- Microsoft:Team Foundation Server 20XX Release Notes
ylbtech-Microsoft:Team Foundation Server 2017 Release Notes 1.返回顶部 1. https://docs.microsoft.com/en- ...
- php5.4编译安装--nginx
1.下载源码包 wget 网址/源码包2.解压源码包 tar -zxvf 源码包3.创建一个安装目录 mkdir /usr/local/php4.进入解压后的目录中,初始化安装环境./configur ...
- apicloud 运费计算js+页面
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 关于Laravel Gate 和 Policies 的理解
他们的关系是什么? Gate 派生出 Policies 原因见:Providers/AuthServiceProvider.php 文件 其中有注册policies方法: public functio ...
- 虚拟化 RemoteApp 远程接入 源码 免费
远程接入 RemoteApp 虚拟化 源码 免费 1.终端安装与配置: 此远程接入组件的运行原理与瑞友天翼.异速连.CTBS等市面上常见的远程接入产品一样,是透过Windows的终端服务来实现的,速度 ...
- 通俗易懂的理解 Redux(知乎)
1. React有props和state: props意味着父级分发下来的属性[父组件的state传递给子组件 子组件使用props获取],state意味着组件内部可以自行管理的状态,并且整个Rea ...