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. Python决定一个变量时局部的,还是全局的,是在编译期

    Python中的变量名是在编译时就解析好的,换句话说,在编译时(也就是在交互控制台输入代码是或者import文件时),Python就已经决定一个变量应该是局部变量,还是全局变量.来看下面的例子: &g ...

  2. Fox and Number Game

    Fox Ciel is playing a game with numbers now. Ciel has n positive integers: x1, x2, ..., xn. She can ...

  3. TCP系列20—重传—10、早期重传(ER)

    一.介绍 在前面介绍thin stream时候我们介绍过有两种场景下可能不会产生足够的dup ACK来触发快速重传,一种是游戏类响应交互式tcp传输,另外一种是传输受到拥塞控制的限制,只能发送少量TC ...

  4. TCP系列17—重传—7、SACK下的重传

    我们之前介绍SACK选项的时候说过,SACK可以把接收端系列号空间的洞反映给发送端,因此发送端可以更充分的理解接收端的情况,而进行更好的重传恢复过程.这种过程有时候也叫做advanced loss r ...

  5. zabbix概念

    Zabbix是一个企业级的.开源的.分布式监控解决方案. Zabbix可以监控网络和服务的监控状况. Zabbix利用灵活的告警机制,允许用户对事件发送基于Email的告警. 这样可以保证快速的对问题 ...

  6. 关闭win7/Server 2008非正常关机启动自动修复功能

    命令提示符下输入 bcdedit /set {default} bootstatuspolicy ignoreallfailures bcdedit /set {current} recoveryen ...

  7. 伟大的淘宝IP库的API接口竟然提示503挂掉了

    1 淘宝IP库惊现503错误 吃完晚饭,大概6点半了,天色已暗,太阳早就落山了.回到宿舍打开博客一看,傻眼了:博客每篇文章的评论者的地理信息全部处于“正在查询中……”的状态.这神马情况,不会是被淘宝封 ...

  8. dedecms给原模型添加新字段

    1.进入dedecms后台 2.点击核心=>频道模型=>内容模型管理(在这里可以看到dedecms预设的模型设置) 3.选中我们需要的模型,点击更改,跳入以下页面 4.点击字段管理(可以看 ...

  9. [OS] 进程间通信--管道

    管道是单向的.先进先出的.无结构的.固定大小的字节流,它把一个进程的标准输出和另一个进程的标准输入连接在一起.写进程在管道的尾端写入数据,读进程在管道的首端读出数据.数据读出后将从管道中移走,其它读进 ...

  10. 【python】python字符串前面加u,r,b的含义

    1.字符串前加 u 例:u"我是含有中文字符组成的字符串." 作用:后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时出 ...