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. smb服务器命令

    win +r  win7下运行   //linux主机地址 sudo smbpasswd -a 用户名  添加用户和密码

  2. synchronized四要素及抽象

    面向对象,java的核心思想就是面向对象编程,以贴近人类在现实生活中的思考方式,任何事物我们都会把它抽象成一个对象,一辆车,一个人,对象是我们思考的基石. 我想说的是,我们写汉字的时候,不会在脑海中命 ...

  3. <<小朋友的数字>>核心代码

    for(i=l;i<=n;i++) { a[i]=read(); t=t+a[i]; if (t>maxn) maxn=t if( t<0) t=0; b[i]=maxn; }

  4. eBox(stm32) 之中断结构

    eBox的中断结构参考了mbed,和我们平时所用的中断结构有些差异,不容易理解,最近仔细看了底层代码,终于搞清楚了,总结一下         一  首先要要搞清楚的几个概念:类的静态成员,实例成员   ...

  5. dr.wondr博士随笔之三星某智能机的SGHXXXX 的取证恢复一例

    大家好!欢迎今天再次来到我dr.wonde的博客, 今天我给大家带来一款三星镜面古董机SGH-E848的取证展示! 三星SGH-E848是一款非常漂亮的镜面手机,2008年出厂.. 上面黄色是97号数 ...

  6. CC_STACKPROTECTOR防内核堆栈溢出补丁分析【转】

    转自:https://yq.aliyun.com/articles/1723 摘要: 作者:王智通   CC_STACKPROTECT补丁是Tejun Heo在09年给主线kernel提交的一个用来防 ...

  7. Java数据结构之字符串模式匹配算法---KMP算法2

    直接接上篇上代码: //KMP算法 public class KMP { // 获取next数组的方法,根据给定的字符串求 public static int[] getNext(String sub ...

  8. 《编写可维护的JavaScript》——JavaScript编码规范(二)

    昨天是我偶像生日,现在整个人都还好兴奋啊O(∩_∩)O~  闲话少说,让我先发篇随笔留念一下^_^ ////////////////////////////////正文分割线///////////// ...

  9. html(二)

    1无序列表 ul是没有前后顺序的信息列表. <ul> <li></li> <li></li> ...... </ul> ul在网 ...

  10. Unity中各类物理投射性能横向比较

    最近在优化摄像机部分代码,抽了个时间对物理投射这块进行了系统性的测试,发现了不少东西 测试工程下载地址: http://files.cnblogs.com/files/hont/RaycastTest ...