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

题意:经典的求最大连续子序列和一点点的加难版。
题解:做法很多,数据结构的书介绍时间复杂度时作为例题分析过,除了O(n^2)的DP外可以用分治,以及题目要求的特殊性可以使用O(n)的算法。
 #include <stdio.h>
#include <iostream>
using namespace std; int a[];
int main()
{
int n, flag = ;
scanf("%d", &n);
for(int i = ; i < n; i++)
{
scanf("%d", a + i);
if(a[i] >= )
flag = ;
}
int ans = ;
int tl, tr, t;
int l, r, ma;
l = r = ;
tl = tr = t = ;
//t = a[0];
ma = ;
for(int i = ; i < n; i++)
{
if(t + a[i] <= )
{
if(t > ma)
{
ma = t;
l = tl;
r = tr;
}
t = ;
tl = i + ;
tr = i + ;
}
else
{
t += a[i];
tr = i;
if(t > ma)
{
ma = t;
l = tl;
r = tr;
} }
//cout << l << "~" << r << endl;
}
if(ma > )
printf("%d %d %d\n", ma, a[l], a[r]);
else if(ma== && flag) //特判 -1 0 -1 情况
printf("0 0 0\n");
else printf("0 %d %d\n", a[], a[n-]); }

 

PAT (Advanced Level) 1007. Maximum Subsequence Sum (25) 经典题的更多相关文章

  1. PAT (Advanced Level) 1007. Maximum Subsequence Sum (25)

    简单DP. 注意:If all the K numbers are negative, then its maximum sum is defined to be 0, and you are sup ...

  2. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

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

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

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

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

  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)(25 分)(0不是负数,水题)

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

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

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

  8. 1007 Maximum Subsequence Sum (25 分)

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

  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. HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)

    Description Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “im ...

  2. jquery中的$(document).ready()、JavaScript中的window.onload()以及body中的onload()、DomContentLoaded()区别

    $().ready().$(handler).$(document).ready(handler)均不是原生JS中的,都是jQuery中封装的方法.这些事件在当页面的dom节点加载完毕后就执行,无需等 ...

  3. 业务迁移---redis

    以前也没怎么搞过redis 只知道他是一个nosql数据库很强大,这次迁移用到了~  正好熟练一下并记录过程,还挺繁琐.. 记录一下在学习中的几个问题,总结加深一下印象,有可能会漏掉或者有误差的地方~ ...

  4. 使用HTML5制作loading图

    昨天发了一篇使用HTML5 canvas写的时钟的文章,今天发一篇关于使用HTML5制作loading图的文章. <!DOCTYPE html> <html> <head ...

  5. 《梦断代码Dreaming In Code》阅读笔记(二)

    这段时间一口气读了5章,所以想着现在一块写阅读笔记. 在阅读的这段时间,我一直是晚上进行阅读,很多时候都是读完一看已经一个小时了,效果还不错.闲话不表,说说阅读心得. 关于底层语言或是低级语言,我之前 ...

  6. 软件工程课堂作业(一)——随机产生四则运算题目(C++)

    一.设计思想: 1.首先主函数只用来调用随机产生并输出运算题目函数,随机产生并输出这一部分功能用一个randout函数实现: 2.随机产生运算数这一功能,两个运算数可以用随机函数生成,并将它们控制在1 ...

  7. C#常见函数

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. vc6.0批量加注释

    MATLAB批量加注释的方法非常简单明了,加注释是ctrl+R,去注释是ctrl+T 然后在VC中我对一条一条加注释的方法非常烦恼,我想也许会有简单的方法可以批量家注释.果然,先贴代码 '------ ...

  9. window service 创建

    1:vs中创建一个 window servece 2.右键 添加安装程序 3.更改属性视图中的Account属性为LocalService(本地服务) 更改ServiceName为你自己的服务名称   ...

  10. 第70天:jQuery基本选择器(一)

    一.jQuery基本选择器 jQuery是javascript的一个库,包含多个可重用的函数,用来辅助我们简化javascript开发 jQuery能做的javascipt都能做到,而javascri ...