Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. 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 (<= 10000). 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

题本身不难,但是想了好久竟然没有思路... 也是在看了别人的代码后才写出来通过的
遍历两边,一边遍历一边记录下最大的和以及开始和结束的数字。
出了一次问题,就是当序列中最大的值是0的时候怎样正确输出 0 0 0
#include <iostream>

using namespace std;

int a[];

int main()
{
int temp,k,start,end1,sum;
sum=-;
cin>>k;
for(int i=;i<k;i++){
cin>>a[i];
}
for(int i=;i<k;i++){
temp=;
for(int j=i;j<k;j++){
temp=temp+a[j];
if(temp>sum){
sum=temp;
start=a[i];
end1=a[j];
}
}
}
if(sum<=-)
cout<<<<" "<<a[]<<" "<<a[k-];
else
cout<<sum<<" "<<start<<" "<<end1; return ;
}

1007. Maximum Subsequence Sum (25)的更多相关文章

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

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

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

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

  4. 1007 Maximum Subsequence Sum (25 分)

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

  5. PAT 解题报告 1007. Maximum Subsequence Sum (25)

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

  6. 连续子数组的最大和/1007. Maximum Subsequence Sum (25)

    题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...

  7. 1007 Maximum Subsequence Sum (25)(25 point(s))

    problem Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A continuous subsequence is define ...

  8. PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 经典题

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

  9. PAT Advanced 1007 Maximum Subsequence Sum (25 分)

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

随机推荐

  1. 同步github上fork出来的分支

    1,clone自己的账号里fork库的分支到本地 git clone 地址_branch 2,增加远程原始分支 git remote add upstream 地址_master 3,fetch原始源 ...

  2. 第九章伪代码编程过程 The PseudoCode Programming Process

    目录: 1.创建类和子程序的步骤概述 2.伪代码 3.通过伪代码编程过程创建子程序 4.伪代码编程过程的替代方案 一.创建类和子程序的步骤概述 (1)创建一个类的步骤 1.创建类的总体设计 2.创建类 ...

  3. poj 3254 Corn Fields

    http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  4. android 流量 压缩

    引用:http://my.eoe.cn/blue_rain/archive/340.html 对于目前的状况来说,移动终端的网络状况没有PC网络状况那么理想.在一个Android应用中,如果需要接收来 ...

  5. 配置Report Server超时

    http://blogs.msdn.com/b/mariae/archive/2009/09/24/troubleshooting-timeout-errors-in-reporting-servic ...

  6. java 建造者模式

    package de.bvb.test3; /** * 建造者模式: 假如一个类有4个字段,每个字段或者每几个字段的组合都需要设置为构造函数,构造函数就比较麻烦 * 而且如果再加一个字段进去也不好拓展 ...

  7. C#:额外知识点

    6.写入.输出二进制数据(指定地址 与 内存交互数据) public void WriteSerialize(object data, int offset, int length) { Binary ...

  8. 向苹果App Store提交新应用的图文教程(转)

    当你克服重重困难终于开发出了自己的App,下一步就是向App Store提交应用了,这时应该如何操作呢?我的App真的准备好提交了?我敢肯定这些问题将会浮现在你的脑海.基于这篇教程,我将告诉你一个完整 ...

  9. c#实现邮件发送链接激活

    2016-08-24 10:09:52 public void MailSend(string email) { MailMessage MyMail = new MailMessage(); MyM ...

  10. 前端之CSS(二)

    一.盒子模型 说到盒子模型,我们不得不提一下,W3C标准和IE浏览器是有区别的,我昨天就在写抽屉作业的时候踩过坑,建议用谷歌浏览器,并推荐一篇博文:http://www.osmn00.com/tran ...