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()
{
/**计算时间
* 开始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 分)的更多相关文章

  1. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

    1007 Maximum Subsequence Sum (25分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  2. 1007 Maximum Subsequence Sum (25 分)

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  3. PAT Advanced 1007 Maximum Subsequence Sum

    题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...

  4. 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 ...

  5. 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 ...

  6. PAT 1007 Maximum Subsequence Sum (25分)

    题目 Given a sequence of K integers { N​1​​ , N​2​​ , ..., N​K​​ }. A continuous subsequence is define ...

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

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

  8. 【PAT甲级】1007 Maximum Subsequence Sum (25 分)

    题意: 给出一个整数K(K<=10000),输入K个整数.输出最大区间和,空格,区间起点的数值,空格,区间终点的数值.如果有相同的最大区间和,输出靠前的.如果K个数全部为负,最大区间和输出0,区 ...

  9. PAT 甲级 1007. Maximum Subsequence Sum (25) 【最大子串和】

    题目链接 https://www.patest.cn/contests/pat-a-practise/1007 思路 最大子列和 就是 一直往后加 如果 sum < 0 就重置为 0 然后每次 ...

随机推荐

  1. 2018-2019-2-20175225 实验三 《敏捷开发与XP实践》实验报告

    一.实验内容与步骤 1.安装.使用alibaba插件规范代码 - 在IDEA的setting中找到plugins并搜索alibaba,点击install进行安装 - 重启IDEA后,在代码中右击点击编 ...

  2. kurento搭建以及运行kurento-hello-world

    搭建环境的系统是ubuntu 1.kurento服务器搭建 运行如下脚本即可完成安装 #!/bin/bash echo "deb http://ubuntu.kurento.org trus ...

  3. Xcode升级10.3后XIB文件报错

    Xcode升级10.3后XiB文件包错,提示: Failed to find or create execution context for description '<IBCocoaTouch ...

  4. 二、Jmter查看结果数只能显示有限的数据,查看全部数据

    1.打开jmeter安装目录,找到bin目录下jmeter.properties文件 2.搜索:view.results.tree.max_size=10485760 3.将#号去掉,重启jmeter

  5. 【flask-Email】邮件发送

    使用依赖: flask_mail 安装方式: pip3 install flask-mail 代码示例: from flask import Flask from flask_mail import ...

  6. MySQL单列索引和组合索引的创建及区别介绍

    MySQL单列索引是我们使用MySQL数据库中经常会见到的,MySQL单列索引和组合索引的区别可能有很多人还不是十分的了解,下面就为您分析两者的主要区别,供您参考学习. 为了形象地对比两者,再建一个表 ...

  7. Java中个容器的对比

    List: 有序,列表存储,元素可重复 Set: 无序,元素不可重复 Map:无序,元素可重复,key不能重复 LinkedList :链表,删除和添加效率很高,随机访问效率较ArrayList类低, ...

  8. 梳理检测论文-Refinement Neural Network

    Single-Shot Refinement Neural Network for Object Detection 目录 1. motivation 2. RefineDet 解析(Network ...

  9. java.lang.NoSuchMethodError: org.apache.spark.internal.Logging.$init$(Lorg/apache/spark/internal/Logging;)V

    1.sparkML的版本不对应 请参考官网找到对于版本, 比如我的 spark2.3.3          spark MLlib 也是2.3.3

  10. centos 7 中如何提取IP地址

    ifconfig |grep -Eo "(([1-9)?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|1[0-9]{2}|2[0-4][0 ...