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. Css定位之relative_慕课网课程笔记

    前言 最近在慕课网上跟着张鑫旭大神重新学习一遍CSS相关的知识,以下是学习的笔记以及个人一些理解 relative对绝对定位的限制 1.限制绝对定位 绝对定位的top.left.right和botto ...

  2. Android下OpenCV的环境搭建

    目录(?)[-] 前言 系统环境 相关工具 Android ADT环境搭建 Android SDK环境变量的配置 Android NDK的安装与配置 OpenCV for Android 环境搭建 基 ...

  3. Eclipse的安装与调试

    1.首先进入eclipse官网下载eclipse 2.根据自己的系统选择相应版本进行下载.由于我的是64bit,所以我选择64bit版下载. 3.点开下载的程序,进行安装. 4.随后进入了安装页面.和 ...

  4. jquery cdn/////////////////zzz

    jquery-2.1.1 注:jquery-2.0以上版本不再支持IE 6/7/8)百度引用地址 (推荐目前最稳定的,不会出现延时打不开情况) 百度压缩版引用地址:<script src=&qu ...

  5. PHP $_SERVER详解

    $_SERVER['HTTP_ACCEPT_LANGUAGE']//浏览器语言 $_SERVER['REMOTE_ADDR'] //当前用户 IP . $_SERVER['REMOTE_HOST'] ...

  6. Find cmd

    Find cmd with python programing python at page320 Unix find cmd: find . -name "*.py " -pri ...

  7. Hdu 1214 圆桌会议

    圆桌会议 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. R 操作矩阵和计算SVD的基本操作记录

    在R中可以用函数matrix()来创建一个矩阵,应用该函数时需要输入必要的参数值. > args(matrix) function (data = NA, nrow = 1, ncol = 1, ...

  9. Python导入Scipy子模块时出错

    导入Scipy子模块时报错,出现的问题都是提示 61 from numpy._distributor_init import NUMPY_MKL  # requires numpy+mklNo mod ...

  10. Windows下部署Appium教程(Android App自动化测试框架搭建)

    摘要: 1,appium是开源的移动端自动化测试框架: 2,appium可以测试原生的.混合的.以及移动端的web项目: 3,appium可以测试ios.android.firefox os: 4,a ...